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 J.
E: [email protected]

On 7/24/06, Ben J. [email protected] 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)

Hi –

On Mon, 24 Jul 2006, Phillip H. wrote:

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

You would use __send__' in the eventsend’ is overriden.

‘neb’.send(:reverse) #=> ‘ben’

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs