# Newbie question: variable method call

**URL:** <https://www.ruby-forum.com/t/newbie-question-variable-method-call/105146>\
**Category:** Ruby\
**Created:** [May 26, 2007, 7:50am UTC](https://www.ruby-forum.com/t/newbie-question-variable-method-call/105146 "2007-05-26T07:50:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9de053/32.png) [@kal](https://www.ruby-forum.com/u/kal)\
**Post date:** [May 26, 2007, 7:50am UTC](https://www.ruby-forum.com/t/newbie-question-variable-method-call/105146/1 "2007-05-26T07:50:41Z")

</div>

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’).

---

<div class="post-metadata">

**Author:** ![kal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9de053/32.png) [@kal](https://www.ruby-forum.com/u/kal)\
**Post date:** [May 26, 2007, 7:58am UTC](https://www.ruby-forum.com/t/newbie-question-variable-method-call/105146/2 "2007-05-26T07:58:28Z")

</div>

On May 25, 2007, at 10:50 PM, Kal S. wrote:

> elsif action == “pat”  
> pet.pat  
> #etc…  
> end
> 
> I’d rather just do something like this:
> 
> pet.action

pet.send action

Cheers-  
– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![kal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9de053/32.png) [@kal](https://www.ruby-forum.com/u/kal)\
**Post date:** [May 26, 2007, 8:07am UTC](https://www.ruby-forum.com/t/newbie-question-variable-method-call/105146/3 "2007-05-26T08:07:07Z")

</div>

Thanks Ezra!
