Dynamically call object instances

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

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 | mentoring
book: “The Well-Grounded Rubyist” | code review
http://www.manning.com/black2 | manuscript review

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

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.

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

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
[email protected]