# Re: Newbie question: variable method call

**URL:** <https://www.ruby-forum.com/t/re-newbie-question-variable-method-call/105148>\
**Category:** Ruby\
**Created:** [May 26, 2007, 8:06am UTC](https://www.ruby-forum.com/t/re-newbie-question-variable-method-call/105148 "2007-05-26T08:06:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthieu\_TC](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@Matthieu\_TC](https://www.ruby-forum.com/u/Matthieu_TC)\
**Post date:** [May 26, 2007, 8:06am UTC](https://www.ruby-forum.com/t/re-newbie-question-variable-method-call/105148/1 "2007-05-26T08:06:28Z")

</div>

Assuming your variable contains a string, try this:

method\_name = “feed”  
pet.send(method\_name)

-mats

----- Original Message ----  
From: Kal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Saturday, May 26, 2007 3:50:41 PM  
Subject: Newbie question: variable method call

Is there a way to use the value of a variable to call a method of that  
name?

For example, rather than use the following code to determine which  
method of the ‘pet’ object to call:

if action == “feed”  
pet.feed  
elsif action == “walk”  
pet.walk  
elsif action == “pat”  
pet.pat  
#etc…  
end

I’d rather just do something like this:

pet.action

The problem is, Ruby thinks I am trying to call a method named ‘action’,  
instead of evaluating the variable and using its value (e.g. ‘feed’).
