[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Passing method name to method?

Arfon Smith

9/28/2007 3:25:00 PM

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
--
Posted via http://www.ruby-....

3 Answers

Logan Capaldo

9/28/2007 3:34:00 PM

0

On 9/28/07, Arfon Smith <arfon.smith@gmail.com> wrote:
> 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)

> Any idea where I'm going wrong?
>
> Thanks
> --
> Posted via http://www.ruby-....
>
>

Austin Ziegler

9/28/2007 3:35:00 PM

0

On 9/28/07, Arfon Smith <arfon.smith@gmail.com> wrote:
> 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
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Arfon Smith

9/28/2007 3:39:00 PM

0

Logan Capaldo wrote:
> On 9/28/07, Arfon Smith <arfon.smith@gmail.com> wrote:
>> 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)

It works!

Thanks. So I have to pass a symbol to the method to get this to work?
--
Posted via http://www.ruby-....