views:

22

answers:

2

I have this helper that I am building:

def myhelper(object, attributes = [])
  attributes.each do |attr|
    object.attr
  end
end

I invoke this helper using:

myhelper Person, [:title, :name]

What I am trying to achieve is to print a list of attributes in Person dynamically but object.attr in myhelper method won't work.

How can I do that?

+2  A: 

Try object.send(attr)

Also, do you want to do a map instead?

jordinl
I refined the code and used collection but send(attr) is what I needed to know. Thank you.
Omar
A: 

Is Person an ActiveRecord or ActiveModel object, or just an ordinary object?

Also, what do you mean by "won't work"? Are you getting no output, or are you getting an exception?

Andrew Grimm