# Passing method name to method?

**URL:** <https://rubytalk.org/t/passing-method-name-to-method/41006>\
**Category:** ruby-talk\
**Created:** [28 September 2007 15:24 UTC](https://rubytalk.org/t/passing-method-name-to-method/41006 "2007-09-28T15:24:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arfon\_Smith](https://avatars.discourse-cdn.com/v4/letter/a/a88e57/32.png) [@Arfon\_Smith](https://rubytalk.org/u/Arfon_Smith)\
**Post date:** [28 September 2007 15:24 UTC](https://rubytalk.org/t/passing-method-name-to-method/41006/1 "2007-09-28T15:24:53Z")

</div>

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)  
&nbsp;&nbsp;puts "#{[object.id](http://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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [28 September 2007 15:33 UTC](https://rubytalk.org/t/passing-method-name-to-method/41006/2 "2007-09-28T15:33:52Z")

</div>

> 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)  
&nbsp;&nbsp;puts "#{object\_id}" if object.send(param) == true  
end

puts objects.list\_by\_param(:param)

> **···**
>
> On 9/28/07, Arfon Smith \<arfon.smith@gmail.com\> wrote:
> 
> > Any idea where I'm going wrong?
> > 
> > Thanks  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler5](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/austin_ziegler5/32/1985_2.png) [@Austin\_Ziegler5](https://rubytalk.org/u/Austin_Ziegler5)\
**Post date:** [28 September 2007 15:34 UTC](https://rubytalk.org/t/passing-method-name-to-method/41006/3 "2007-09-28T15:34:40Z")

</div>

class Object  
&nbsp;&nbsp;def objid\_if\_param(param)  
&nbsp;&nbsp;&nbsp;&nbsp;"#{self.\_\_id\_\_}" if self.\_\_send\_\_(param) == true  
&nbsp;&nbsp;end  
end

class Foo  
&nbsp;&nbsp;attr\_accessor :foo  
end

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

[bar, baz].each do |ob|  
&nbsp;&nbsp;id = ob.objid\_if\_param(:foo)  
&nbsp;&nbsp;puts id if id  
end

-austin

> **···**
>
> 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.
> 
> --  
> Austin Ziegler \* halostatue@gmail.com \* [http://www.halostatue.ca/](http://www.halostatue.ca/)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* austin@halostatue.ca \* [You are in a maze of twisty little passages, all alike. // halo • statue](http://www.halostatue.ca/feed/)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\* austin@zieglers.ca

---

<div class="post-metadata">

**Author:** ![Arfon\_Smith](https://avatars.discourse-cdn.com/v4/letter/a/a88e57/32.png) [@Arfon\_Smith](https://rubytalk.org/u/Arfon_Smith)\
**Post date:** [28 September 2007 15:38 UTC](https://rubytalk.org/t/passing-method-name-to-method/41006/4 "2007-09-28T15:38:44Z")

</div>

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)  
> > &nbsp;&nbsp;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/\](http://www.ruby-forum.com/%5C).
