Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7647007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:21:22+00:00 2026-05-31T10:21:22+00:00

I have a method that takes a condition and item, from an array of

  • 0

I have a method that takes a condition and item, from an array of conditions and array of items, where any number of conditions are checked against any number of items. Conditions and Items are Hashes Basically, for the condition and item fed to the method, find out what attribute of the item the condition needs to check against. The list is actually longer, and looking into making this better (seems like this could be more concise and perhaps more rubyesque) nothing else has worked(yet), so I’d like some input on how you might refactor this:

  def check_condition(condition, item)
    case condition.attribute
    when :author
      i = item.author.name;
    when :title
      i = item.title
    when :body
      i = item.body
    when :domain
      i = URI(item.url).host
    when :account_age
      i = item.author.author_age
    end
    @logger.info "#{i} to be checked if #{condition.query} #{condition.attribute}"
    test_condition(condition, item, i)
  end

EDIT:

Just to make clearer, items and conditions are Hashes (Hashie::Mash to be exact) where conditions are in general constructed from a config file that might be something like:

[submitted_link, account_age, is_less_than, 30, remove]

that ends up something like:

{subject: submitted_link, attribute: account_age, query: is_less_than, what: 30 action:remove}

And you can see what is going on as a whole here if you are so inclined: https://github.com/blueblank/reddit_modbot/blob/master/lib/modbot/modbot_check.rb

EDIT2:

The reality of the solution was to somewhat regularize my variable terminology for condition and item so this could be reduced to 1 line

 i = item.send(condition.attribute)

no mess, minimal impact

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:21:23+00:00Added an answer on May 31, 2026 at 10:21 am

    One alternative might involve defining check_condition as a method of item.class. So instead of…

    x = check_condition(c, item)
    

    …you might have something like…

    x = item.condition(c)
    

    Then, if you don’t like the big case, you could create a hash with values that are proc objects, keyed by :author, :title, etc.

    class A
      def initialize
        @h = { :a => proc { @author } }
      end
      def set x
        @author = x
      end
      def f x
        @h[x].call
      end
    end
    
    o = A.new
    o.set 'Me'
    p(o.f(:a))
    

    However, once you do that, you might then want to take a third step, which would be to change all those object attributes to values in a Hash in the first place … maybe …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I want to have a method that takes any kind of number, is
I have a method that takes an array of queries, and I need to
I have a method that takes an IQueryable. Is there a LINQ query that
I have a method that takes a list of entities ( Class es) and
Suppose I have a method that takes an object of some kind as an
I'm wondering how to go about testing this. I have a method that takes
I have an init method that takes an (id) argument: -(id) initWithObject:(id) obj; I'm
I have a service method that takes a String and then replaces tags in
I have a search method that takes in a user-entered string, splits it at
I have a static method that takes a parameter and returns a class. the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.