# Passing by value of object of a model

**URL:** <https://www.ruby-forum.com/t/passing-by-value-of-object-of-a-model/103501>\
**Category:** Ruby\
**Created:** [May 7, 2007, 9:09am UTC](https://www.ruby-forum.com/t/passing-by-value-of-object-of-a-model/103501 "2007-05-07T09:09:55Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![arjun\_ghosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@arjun\_ghosh](https://www.ruby-forum.com/u/arjun_ghosh)\
**Post date:** [May 7, 2007, 9:09am UTC](https://www.ruby-forum.com/t/passing-by-value-of-object-of-a-model/103501/1 "2007-05-07T09:09:55Z")

</div>

Hi,  
can anyone tell me - is it possible to pass a string name equivalent of  
a  
model name(example:- have a model called food\_dept and i am passing the  
string “food\_dept” to a method) and then covert(or cast) it to its  
original  
model equivalent so that i can use its object in the called method. and  
why  
i am i trying to this manner is because i want to avoid passing an  
object  
which will make it very heavy. i think there should be some better  
elegant  
manner of doing this. can any one help me with this?  
ciao  
-AG

---

<div class="post-metadata">

**Author:** ![arjun\_ghosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@arjun\_ghosh](https://www.ruby-forum.com/u/arjun_ghosh)\
**Post date:** [May 7, 2007, 9:15am UTC](https://www.ruby-forum.com/t/passing-by-value-of-object-of-a-model/103501/2 "2007-05-07T09:15:56Z")

</div>

On 5/7/07, arjun ghosh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> >

“food\_dept”.classify.constantize =\> FoodDept

Pat

---

<div class="post-metadata">

**Author:** ![arjun\_ghosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@arjun\_ghosh](https://www.ruby-forum.com/u/arjun_ghosh)\
**Post date:** [May 7, 2007, 9:22am UTC](https://www.ruby-forum.com/t/passing-by-value-of-object-of-a-model/103501/3 "2007-05-07T09:22:43Z")

</div>

arjun ghosh wrote:

> Hi,  
> can anyone tell me - is it possible to pass a string name equivalent of  
> a  
> model name(example:- have a model called food\_dept and i am passing the  
> string “food\_dept” to a method) and then covert(or cast) it to its  
> original  
> model equivalent so that i can use its object in the called method. and  
> why  
> i am i trying to this manner is because i want to avoid passing an  
> object  
> which will make it very heavy. i think there should be some better  
> elegant  
> manner of doing this. can any one help me with this?  
> ciao  
> -AG

I know how to do it for RoR.  
If food\_dept is a model(class) name,shouldn’t it be FoodDept?  
if so, you can use this:  
“food\_dept”.camelize.constantize

if you mean it’s a method, use this:  
send(“food\_dept”)

> > class Foo  
> > def bar  
> > “test”  
> > end  
> > end  
> > =\> nil
> 
> > a=“foo”  
> > =\> “foo”
> 
> > b=“bar”  
> > =\> “bar”
> 
> > c = a.camelize.constantize.new  
> > =\> #Foo:0xb76cfa80
> 
> > c.send(b)  
> > =\> “test”

but sadly, it seems this wouldn’t work for ruby.

---

<div class="post-metadata">

**Author:** ![arjun\_ghosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@arjun\_ghosh](https://www.ruby-forum.com/u/arjun_ghosh)\
**Post date:** [May 7, 2007, 9:24am UTC](https://www.ruby-forum.com/t/passing-by-value-of-object-of-a-model/103501/4 "2007-05-07T09:24:56Z")

</div>

Thanks pat for the quick help…can u point me to a directions where i  
can  
learn these things…a book or pdf…it is embarrassing to ask these  
simple  
stuff from u guys…though thanks again  
-ciao  
AG

---

<div class="post-metadata">

**Author:** ![arjun\_ghosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@arjun\_ghosh](https://www.ruby-forum.com/u/arjun_ghosh)\
**Post date:** [May 7, 2007, 9:42am UTC](https://www.ruby-forum.com/t/passing-by-value-of-object-of-a-model/103501/5 "2007-05-07T09:42:57Z")

</div>

On 7 May 2007, at 16:22, Nanyang Z. wrote:

> > method. and  
> > If food\_dept is a model(class) name,shouldn’t it be FoodDept?
> > 
> > > end  
> > > but sadly, it seems this wouldn’t work for ruby.  
> > > The ‘const\_get’ method will return a constant given a symbol (use  
> > > to\_sym to turn a String into a Symbol):

irb(main):001:0\> class Foo  
irb(main):002:1\> def moon; ‘gibbous’; end  
irb(main):003:1\> end  
=\> nil  
irb(main):004:0\> class Bar  
irb(main):005:1\> def moon; ‘waning’; end  
irb(main):006:1\> end  
=\> nil  
irb(main):007:0\> Object.const\_get(:Foo).new.moon  
=\> “gibbous”  
irb(main):008:0\> Object.const\_get(:Bar).new.moon  
=\> “waning”

A specific comment for the original poster though: Ruby objects are  
passed by reference so unless I misunderstand your question you  
shouldn’t worry about passing ‘heavy’ objects. Though without a code  
example it is hard to know exactly what you want.

Alex G.

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![arjun\_ghosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@arjun\_ghosh](https://www.ruby-forum.com/u/arjun_ghosh)\
**Post date:** [May 7, 2007, 12:13pm UTC](https://www.ruby-forum.com/t/passing-by-value-of-object-of-a-model/103501/6 "2007-05-07T12:13:55Z")

</div>

Hi Nanyang,  
u r correct, my mistake…model name is FoodDept but the string i want  
to  
pass is “food\_dept”. The reason is that there are different dept under a  
model Dept and FoodDept is an inherited model from it… now for my app,  
i  
need to send different dept to a method for processing and hence i was  
trying to send the string name.

---

<div class="post-metadata">

**Author:** ![arjun\_ghosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@arjun\_ghosh](https://www.ruby-forum.com/u/arjun_ghosh)\
**Post date:** [May 7, 2007, 10:11am UTC](https://www.ruby-forum.com/t/passing-by-value-of-object-of-a-model/103501/7 "2007-05-07T10:11:00Z")

</div>

On 5/7/07, arjun ghosh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks pat for the quick help…can u point me to a directions where i can  
> learn these things…a book or pdf…it is embarrassing to ask these simple  
> stuff from u guys…though thanks again  
> -ciao  
> AG

Ruby for Rails is probably the best Ruby book out there, in my  
opinion. It’s not really meant to teach you Rails in depth (that’s  
what Agile WebDev w/Rails is for), but rather to show and explain the  
Ruby that actually makes Rails possible. Open classes, duck typing,  
metaprogramming, etc.

However I’ll also say that I think it’s perfectly acceptable to ask  
that particular question on this list 🙂 It gets asked every once in  
a while, but I couldn’t find anything with a quick google. You should  
spend more time than I did probably 🙂 but you also shouldn’t be  
embarrassed to ask a question.

Pat

---

<div class="post-metadata">

**Author:** ![arjun\_ghosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@arjun\_ghosh](https://www.ruby-forum.com/u/arjun_ghosh)\
**Post date:** [May 7, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/passing-by-value-of-object-of-a-model/103501/8 "2007-05-07T16:15:14Z")

</div>

hi,  
i realized that from Alex’s msg. Though Pat’s info helped me to learn  
this  
way of discovering the class from a name. i finally have used the object  
reference method as it seemed more intutive and simpler as i already had  
the  
object for the above said model.  
Any way thanks to all for the quick help. This is also one thing i like  
about RoR - awsome community support…🙂  
ciao.  
A quick question - This is for Pat and Nanyang –  
Pat u told about “food\_dept”.classify.constantize  
and Nanyang u told the method “food\_dept”.camelize.constantize and when  
i  
ran it in console and checked for class, both gave me class type. so  
whats  
the diffrence?

-AG

---

<div class="post-metadata">

**Author:** ![arjun\_ghosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@arjun\_ghosh](https://www.ruby-forum.com/u/arjun_ghosh)\
**Post date:** [May 7, 2007, 2:11pm UTC](https://www.ruby-forum.com/t/passing-by-value-of-object-of-a-model/103501/9 "2007-05-07T14:11:01Z")

</div>

On 5/7/07, arjun ghosh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,  
> can anyone tell me - is it possible to pass a string name equivalent of a  
> model name(example:- have a model called food\_dept and i am passing the  
> string “food\_dept” to a method) and then covert(or cast) it to its original  
> model equivalent so that i can use its object in the called method. and why  
> i am i trying to this manner is because i want to avoid passing an object  
> which will make it very heavy.

Unless I’m missing something (which is usually the case 🙂 ), there  
seems to be a misunderstanding of how variables work in Ruby.

When you pass food\_dept (or FoodDept) to another method, you are not  
passing the object itself, but rather a variable which holds a  
reference to the object. What you suggest, passing its name and then  
discovering the class from its name is far more expensive than simply  
using the variable.

Cheers,  
David

---

<div class="post-metadata">

**Author:** ![arjun\_ghosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@arjun\_ghosh](https://www.ruby-forum.com/u/arjun_ghosh)\
**Post date:** [May 7, 2007, 7:36pm UTC](https://www.ruby-forum.com/t/passing-by-value-of-object-of-a-model/103501/10 "2007-05-07T19:36:42Z")

</div>

The difference is that #classify also makes the string singular. For  
example:

“funky\_chickens”.camelize =\> “FunkyChickens”  
“funky\_chickens”.classify =\> “FunkyChicken”

You can view the source of both methods:  
[http://api.rubyonrails.com/classes/Inflector.html#M001081](http://api.rubyonrails.com/classes/Inflector.html#M001081)  
[http://api.rubyonrails.com/classes/Inflector.html#M001088](http://api.rubyonrails.com/classes/Inflector.html#M001088)

Pat

---

<div class="post-metadata">

**Author:** ![arjun\_ghosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@arjun\_ghosh](https://www.ruby-forum.com/u/arjun_ghosh)\
**Post date:** [May 7, 2007, 7:50pm UTC](https://www.ruby-forum.com/t/passing-by-value-of-object-of-a-model/103501/11 "2007-05-07T19:50:08Z")

</div>

Pat M. wrote:

> On 5/7/07, arjun ghosh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Thanks pat for the quick help…can u point me to a directions where i can  
> > learn these things…a book or pdf…it is embarrassing to ask these simple  
> > stuff from u guys…though thanks again  
> > -ciao  
> > AG
> 
> Ruby for Rails is probably the best Ruby book out there, in my  
> opinion.

While I agree with the extreme grooviness of Ruby for Rails,  
nevertheless it makes no reference to either camelize or constantize. I  
also checked in Programming Ruby and found nothing. Where does one go  
to find these wonderful calls if they are not in the books?

---

<div class="post-metadata">

**Author:** ![arjun\_ghosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@arjun\_ghosh](https://www.ruby-forum.com/u/arjun_ghosh)\
**Post date:** [May 8, 2007, 12:22am UTC](https://www.ruby-forum.com/t/passing-by-value-of-object-of-a-model/103501/12 "2007-05-08T00:22:23Z")

</div>

On May 7, 2007, at 7:50 PM, Lloyd L. wrote:

> While I agree with the extreme grooviness of Ruby for Rails,  
> nevertheless it makes no reference to either camelize or  
> constantize. I  
> also checked in Programming Ruby and found nothing. Where does one go  
> to find these wonderful calls if they are not in the books?

Some are documented in the API, the Agile documents also some of  
those. If you develop in Rails, however, it’s a good idea to spend an  
afternoon reading the source of Active Support.

– fxn

---

<div class="post-metadata">

**Author:** ![arjun\_ghosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@arjun\_ghosh](https://www.ruby-forum.com/u/arjun_ghosh)\
**Post date:** [May 8, 2007, 12:38am UTC](https://www.ruby-forum.com/t/passing-by-value-of-object-of-a-model/103501/13 "2007-05-08T00:38:24Z")

</div>

On May 7, 2007, at 1:50 PM, Lloyd L. wrote:

> > Ruby for Rails is probably the best Ruby book out there, in my  
> > opinion.
> 
> While I agree with the extreme grooviness of Ruby for Rails,  
> nevertheless it makes no reference to either camelize or  
> constantize. I  
> also checked in Programming Ruby and found nothing. Where does one go  
> to find these wonderful calls if they are not in the books?

Well, these are calls from ActiveSupport which is one of the pieces  
that make up Rails. You wouldn’t find anything in Programming Ruby  
because this isn’t core Ruby. Now there have been some bits and  
pieces that work their way into core (like Symbol#to\_proc), but  
generally the ActiveSupport stuff is likely to stay in its gem.

It’s too bad that Ruby for Rails doesn’t touch on this (I had to go  
look at the index to see if there was a page reference that I could  
give you, but no).

-Rob

[http://rdoc.caboo.se/doc/classes/ActiveSupport/CoreExtensions/String/](http://rdoc.caboo.se/doc/classes/ActiveSupport/CoreExtensions/String/)  
Inflections.html

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
