How to run variable method names?

Lets say object has a method called id.

How do I do the the following?

method_name = "id"
object.method_name

I know the above doesnt work, but I want method_name to be replaced by its value so it would run id.

Thanks for the help.

Thank You,
Ben Johnson
E: bjohnson@contuitive.com

In this case Ruby is interpreting method_name as the method to call.
Check out Object#send

object.send(method_name.to_sym)

···

On 7/24/06, Ben Johnson <bjohnson@contuitive.com> wrote:

Lets say object has a method called id.

How do I do the the following?

method_name = "id"
object.method_name

I know the above doesnt work, but I want method_name to be replaced
by its value so it would run id.

--
Phillip Hutchings
http://www.sitharus.com/

You would use `__send__' in the event `send' is overriden.

'neb'.__send__(:reverse) #=> 'ben'

···

On 7/24/06, Ben Johnson <bjohnson@contuitive.com> wrote:

Lets say object has a method called id.

How do I do the the following?

method_name = "id"
object.method_name

I know the above doesnt work, but I want method_name to be replaced
by its value so it would run id.

Thanks for the help.

Thank You,
Ben Johnson
E: bjohnson@contuitive.com

--
Matt

Hi --

···

On Mon, 24 Jul 2006, Phillip Hutchings wrote:

On 7/24/06, Ben Johnson <bjohnson@contuitive.com> wrote:

Lets say object has a method called id.

How do I do the the following?

method_name = "id"
object.method_name

I know the above doesnt work, but I want method_name to be replaced
by its value so it would run id.

In this case Ruby is interpreting method_name as the method to call.
Check out Object#send

object.send(method_name.to_sym)

No need to call to_sym -- send will accept a string.

David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
Ruby for Rails => RUBY FOR RAILS (reviewed on
                                     Slashdot, 7/12/2006!)
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
dblack@wobblini.net => me