Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Passing method name to method? (http://www.velocityreviews.com/forums/t844487-passing-method-name-to-method.html)

Arfon Smith 09-28-2007 03:24 PM

Passing method name to method?
 
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-forum.com/.


Logan Capaldo 09-28-2007 03:33 PM

Re: Passing method name to method?
 
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-forum.com/.
>
>



Austin Ziegler 09-28-2007 03:34 PM

Re: Passing method name to method?
 
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.halostatue.ca/
* austin@halostatue.ca * http://www.halostatue.ca/feed/
* austin@zieglers.ca


Arfon Smith 09-28-2007 03:38 PM

Re: Passing method name to method?
 
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-forum.com/.



All times are GMT. The time now is 10:52 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57