0

Not really sure how to start asking this question so here's some code.

def find_attendees_by_attribute(attribute, criteria)
    attribute = attribute.to_sym
    if is_accepted_attribute?(attribute)
        @attendee_list.each do |attendee|
            #How do I get attendee."attribute" here?
        end
    else
        puts "Sorry, not a good attribute."
    end
end

def is_accepted_attribute?(attempted_attribute)
    @attribute_list.include?(attempted_attribute)
end

So in the above code I have an Attendee class that has been pushed into the @attendee_list of this AttendeeList class. I'm only doing exact match search for now, I will add case-insensitive and whitespace removal after I get a basic functionality.

I want to have the entered attribute evaluated so that I can get that property from the attendees that are being searched in the block. Let me know if this isn't clear, I obviously lack the terminology of what I'm trying to do.

4

2 回答 2

3

试试这个:

attendee.send(attribute)
于 2013-09-14T04:11:57.787 回答
0

您可能希望用于respond_to?检查您发送的属性是否对该对象有效。我假设@attribute_list是某个具有attributes和的类的实例methods

class User
  attr_accessor :name

  def search_name
    # some logic here
  end
end

u = User.new

#check if `u` can understand or work with search_name
u.respond_to?(:search_name) #=> true
u.respond_to?(:name) #=> true
u.respond_to?(:eat) #=> false
于 2013-09-14T06:17:45.400 回答