# Dynamically call object instances

**URL:** <https://www.ruby-forum.com/t/dynamically-call-object-instances/175367>\
**Category:** Rails\
**Created:** [September 11, 2009, 9:50pm UTC](https://www.ruby-forum.com/t/dynamically-call-object-instances/175367 "2009-09-11T21:50:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [September 11, 2009, 9:50pm UTC](https://www.ruby-forum.com/t/dynamically-call-object-instances/175367/1 "2009-09-11T21:50:43Z")

</div>

Hi again,

I have these functions - about five of them - and they are all the exact  
same code except for which object instances they are referencing. For  
example, if I have a user object and each user can modify one of five  
phone fields - home, work, cell, etc. - I would like to call one  
function and just pass a varibale indicating which one I am modifying.

I tried something like the following with no luck:

def change\_phone(variable)  
…  
@user."#{varibale}" = @phone  
end

where variable was work, for example. But it throws an error “Unexpected  
STRING\_BEG”. If there a way to do what I am doing? Thanks,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [September 11, 2009, 10:05pm UTC](https://www.ruby-forum.com/t/dynamically-call-object-instances/175367/2 "2009-09-11T22:05:04Z")

</div>

Hi –

On Fri, 11 Sep 2009, Shandy N. wrote:

> def change\_phone(variable)  
> …  
> @user.“#{varibale}” = @phone  
> end
> 
> where variable was work, for example. But it throws an error “Unexpected  
> STRING\_BEG”. If there a way to do what I am doing? Thanks,

Yes:

@user.send(“#{variable}=”, @phone)

David

–  
David A. Black | training  
Ruby Power and Light, LLC | consulting  
[http://www.rubypal.com](http://www.rubypal.com) | mentoring  
book: “The Well-Grounded Rubyist” | code review  
[The Well-Grounded Rubyist](http://www.manning.com/black2) | manuscript review

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [September 14, 2009, 7:51pm UTC](https://www.ruby-forum.com/t/dynamically-call-object-instances/175367/3 "2009-09-14T19:51:23Z")

</div>

What if I wanted to use that passed in variable to check and see if the  
field was blank or simple reference the value that that field held and  
not assign it a value?

For example:

if @user."#{params[:variable]}".blank?  
…  
end

where params[:variable] = “home\_phone”. I would want that to translate  
to @user.home\_phone.blank? Is there a way to do that?

Thanks in advance,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [September 14, 2009, 8:54pm UTC](https://www.ruby-forum.com/t/dynamically-call-object-instances/175367/4 "2009-09-14T20:54:31Z")

</div>

Marnen Laibow-Koser wrote:

> Well, the send() syntax that David mentioned would work for that, but if  
> @user is an ActiveRecord object, then you can also pretend it’s a hash  
> and do @user[params[:variable]] .

Thanks for the reply, your suggestion did indeed work.

> The fact that you’re asking these questions indicates that you need to  
> review your basic Ruby and Rails topics. These are elementary  
> questions.

And the last time I checked this forum was open to anybody that wanted  
to ask a legitimate RoR question - noobie or otherwise - and while I  
will admit that I am no ruby expert I am far from a noobie. I have  
answered many questions for beginners of this forum without pointing out  
that “[t]hese are elementary questions” or saying that “you need to  
review your basic Ruby and Rails topics.” I have done this because I was  
once where many people who come to this forum are - without a clue in  
the world and simply trying to better my skill set in the programming  
field by trying to learn something knew, and, in my opinion, pretty  
freakin’ cool. Moreover, I have found this forum extremely helpful and  
an extremely valuable resource because I have felt comfortable to ask  
questions that I didn’t know the answer to - beginner or no - without  
people openly degrading me or other people by saying. . . well, what you  
said in your reply. If I had ever thought that for one moment, that  
somebody was asking a question that anybody who had read the first 5  
pages of a beginning RoR book should, you know what, I kept it to  
myself.

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [September 14, 2009, 11:25pm UTC](https://www.ruby-forum.com/t/dynamically-call-object-instances/175367/5 "2009-09-14T23:25:50Z")

</div>

Shandy N. wrote:

> Hi again,
> 
> I have these functions - about five of them - and they are all the exact  
> same code except for which object instances they are referencing. For  
> example, if I have a user object and each user can modify one of five  
> phone fields - home, work, cell, etc. - I would like to call one  
> function and just pass a varibale indicating which one I am modifying.

This is not a direct answer to your question since the answer has  
already been provided. But, I just wanted to point out that you might be  
struggling with this because your model breaks the First Normal Form  
(1NF) of database design, which states that a table should contain no  
repeating groups.

In this case the repeating groups would be home\_phone, work\_phone  
cell\_phone, etc. These are all phone numbers making up a repeating group  
and should, technically, be normalized, which also cleanly solves the  
problem at hand.

phones  
id | person\_id | label | number  
1 | 1 | home | 555-123-1234  
2 | 1 | work | 555-123-2345

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [September 14, 2009, 8:15pm UTC](https://www.ruby-forum.com/t/dynamically-call-object-instances/175367/6 "2009-09-14T20:15:02Z")

</div>

Shandy N. wrote:

> What if I wanted to use that passed in variable to check and see if the  
> field was blank or simple reference the value that that field held and  
> not assign it a value?
> 
> For example:
> 
> if @user.“#{params[:variable]}”.blank?  
> …  
> end
> 
> where params[:variable] = “home\_phone”. I would want that to translate  
> to @user.home\_phone.blank? Is there a way to do that?

Well, the send() syntax that David mentioned would work for that, but if  
@user is an ActiveRecord object, then you can also pretend it’s a hash  
and do @user[params[:variable]] .

The fact that you’re asking these questions indicates that you need to  
review your basic Ruby and Rails topics. These are elementary  
questions.

> Thanks in advance,
> 
> -S

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
