# How to run variable method names?

**URL:** <https://www.ruby-forum.com/t/how-to-run-variable-method-names/99558>\
**Category:** Ruby\
**Created:** [July 24, 2006, 2:50am UTC](https://www.ruby-forum.com/t/how-to-run-variable-method-names/99558 "2006-07-24T02:50:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 24, 2006, 2:50am UTC](https://www.ruby-forum.com/t/how-to-run-variable-method-names/99558/1 "2006-07-24T02:50:02Z")

</div>

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: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 24, 2006, 2:53am UTC](https://www.ruby-forum.com/t/how-to-run-variable-method-names/99558/2 "2006-07-24T02:53:12Z")

</div>

On 7/24/06, Ben J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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)

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 24, 2006, 3:02am UTC](https://www.ruby-forum.com/t/how-to-run-variable-method-names/99558/3 "2006-07-24T03:02:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![benjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@benjohnson](https://www.ruby-forum.com/u/benjohnson)\
**Post date:** [July 24, 2006, 7:53am UTC](https://www.ruby-forum.com/t/how-to-run-variable-method-names/99558/4 "2006-07-24T07:53:53Z")

</div>

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

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