# Using a variable as substitute for a method

**URL:** <https://www.ruby-forum.com/t/using-a-variable-as-substitute-for-a-method/62944>\
**Category:** Rails\
**Created:** [June 9, 2006, 7:56pm UTC](https://www.ruby-forum.com/t/using-a-variable-as-substitute-for-a-method/62944 "2006-06-09T19:56:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [June 9, 2006, 7:56pm UTC](https://www.ruby-forum.com/t/using-a-variable-as-substitute-for-a-method/62944/1 "2006-06-09T19:56:02Z")

</div>

That probably isn’t the right title for this topic, but I’m not sure how  
else to explain in succintly. I’m sorry if this is obvious but I  
couldn’t find the answer in the Ruby/Rails docs.

How do I use a variable as a substitute for a method/field, so that I  
can call the object with the variable that contains the method name.

For example, say I have the object Product, that has methods Price,  
Last\_Price, Average\_Price.

I want to be able to return the value of either of those three fields  
based on the user selection. Ie, user selection is stored in  
params[:foo], which I assign to variable field\_to\_show, and I want to be  
able to call:

Product.field\_to\_show

which should output Product.Price (if params[:foo] contained ‘Price’)

Currently this gives a no method error.

I know that I can do  
case params[:foo]  
when “Price”  
Product.Price  
etc

but it seems to me that there should be a way of avoiding that extra  
code.

I also tried:

Product.#{field\_to\_show}

and it didn’t work (no method error)

Thansk for the help.

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [June 9, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/using-a-variable-as-substitute-for-a-method/62944/2 "2006-06-09T20:00:05Z")

</div>

I think that Product["#{params[:foo]}"] should do the work.

Roland

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [June 9, 2006, 8:04pm UTC](https://www.ruby-forum.com/t/using-a-variable-as-substitute-for-a-method/62944/3 "2006-06-09T20:04:10Z")

</div>

On 6/9/06, zero halo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That probably isn’t the right title for this topic, but I’m not sure how  
> else to explain in succintly. I’m sorry if this is obvious but I  
> couldn’t find the answer in the Ruby/Rails docs.
> 
> How do I use a variable as a substitute for a method/field, so that I  
> can call the object with the variable that contains the method name.

Generally it’s done by

obj.send(var)

Method calls are just messages send to objects, so you can use the  
#send method to do that.

In ActiveRecords’s case, there’s a special convenience method that  
should work:

ar\_instance[var]

---

<div class="post-metadata">

**Author:** ![zerohalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5fc32e/32.png) [@zerohalo](https://www.ruby-forum.com/u/zerohalo)\
**Post date:** [June 9, 2006, 8:12pm UTC](https://www.ruby-forum.com/t/using-a-variable-as-substitute-for-a-method/62944/4 "2006-06-09T20:12:52Z")

</div>

Alder G. wrote:

> Generally it’s done by
> 
> obj.send(var)
> 
> Method calls are just messages send to objects, so you can use the  
> #send method to do that.
> 
> In ActiveRecords’s case, there’s a special convenience method that  
> should work:
> 
> ar\_instance[var]

Both of those worked. Thanks, Alder.
