Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Passing method name to method?

Reply
Thread Tools

Passing method name to method?

 
 
Arfon Smith
Guest
Posts: n/a
 
      09-28-2007
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/.

 
Reply With Quote
 
 
 
 
Logan Capaldo
Guest
Posts: n/a
 
      09-28-2007
On 9/28/07, Arfon Smith <(E-Mail Removed)> 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(aram)

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


 
Reply With Quote
 
 
 
 
Austin Ziegler
Guest
Posts: n/a
 
      09-28-2007
On 9/28/07, Arfon Smith <(E-Mail Removed)> 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 * http://www.velocityreviews.com/forums/(E-Mail Removed) * http://www.halostatue.ca/
* (E-Mail Removed) * http://www.halostatue.ca/feed/
* (E-Mail Removed)

 
Reply With Quote
 
Arfon Smith
Guest
Posts: n/a
 
      09-28-2007
Logan Capaldo wrote:
> On 9/28/07, Arfon Smith <(E-Mail Removed)> 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(aram)


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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing a Method Name to a Method, Redux Gene Wirchenko Java 51 07-24-2011 10:02 AM
Passing a Method Name to a Method Gene Wirchenko Java 33 06-25-2011 07:26 PM
Passing a method(reference) to an other method and calling themethod. Erik Java 11 03-29-2008 07:26 AM
Passing math method to another method? Neutek Ruby 11 01-18-2007 09:23 AM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 PM



Advertisments