Passing method name to method?

A

Arfon Smith

Hi, sorry if this isn't phrased quite as it should be!

I want to have a generic 'find' method that can check to see if an
object's attribute is true or false.

Basically I want to pass the method name (param) to the list_by_param
method but this doesn't seem to be working (I get an undefined local
variable or method 'param' for main:Object) error.

def list_by_param(param)
puts "#{object.id}" if object.param == true
end

puts objects.list_by_param(param)


Any idea where I'm going wrong?

Thanks
 
L

Logan Capaldo

Hi, sorry if this isn't phrased quite as it should be!

I want to have a generic 'find' method that can check to see if an
object's attribute is true or false.

Basically I want to pass the method name (param) to the list_by_param
method but this doesn't seem to be working (I get an undefined local
variable or method 'param' for main:Object) error.

def list_by_param(param)
puts "#{object.id}" if object.param == true
end

puts objects.list_by_param(param)
I *think* what you are trying to do is

def list_by_param(param)
puts "#{object_id}" if object.send(param) == true
end

puts objects.list_by_param:)param)
 
A

Austin Ziegler

I want to have a generic 'find' method that can check to see if an
object's attribute is true or false.

Basically I want to pass the method name (param) to the list_by_param
method but this doesn't seem to be working (I get an undefined local
variable or method 'param' for main:Object) error.


class Object
def objid_if_param(param)
"#{self.__id__}" if self.__send__(param) == true
end
end

class Foo
attr_accessor :foo
end

bar = Foo.new
baz = Foo.new
baz.foo = true

[ bar, baz ].each do |ob|
id = ob.objid_if_param:)foo)
puts id if id
end

-austin
 
A

Arfon Smith

Logan said:
I *think* what you are trying to do is

def list_by_param(param)
puts "#{object_id}" if object.send(param) == true
end

puts objects.list_by_param:)param)

It works!

Thanks. So I have to pass a symbol to the method to get this to work?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top