Forum: Ruby passing by value of object of a model

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2f515221472e537a7642f0ead9c7772c?d=identicon&s=25 arjun ghosh (Guest)
on 2007-05-07 09:09
(Received via mailing list)
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
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2007-05-07 09:15
(Received via mailing list)
On 5/7/07, arjun ghosh <arjun4ruby@gmail.com> wrote:
>
>  >
>

"food_dept".classify.constantize          => FoodDept

Pat
C91098dc76d7ad473165ef24fe805312?d=identicon&s=25 Nanyang Zhan (xain)
on 2007-05-07 09:22
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.
2f515221472e537a7642f0ead9c7772c?d=identicon&s=25 arjun ghosh (Guest)
on 2007-05-07 09:24
(Received via mailing list)
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
807270f56f26ad90755eef71f2c228fe?d=identicon&s=25 Alex Gutteridge (Guest)
on 2007-05-07 09:42
(Received via mailing list)
On 7 May 2007, at 16:22, Nanyang Zhan 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 Gutteridge

Bioinformatics Center
Kyoto University
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2007-05-07 10:11
(Received via mailing list)
On 5/7/07, arjun ghosh <arjun4ruby@gmail.com> 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
2f515221472e537a7642f0ead9c7772c?d=identicon&s=25 arjun ghosh (Guest)
on 2007-05-07 12:13
(Received via mailing list)
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.
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2007-05-07 14:11
(Received via mailing list)
On 5/7/07, arjun ghosh <arjun4ruby@gmail.com> 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
2f515221472e537a7642f0ead9c7772c?d=identicon&s=25 arjun ghosh (Guest)
on 2007-05-07 16:15
(Received via mailing list)
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
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2007-05-07 19:36
(Received via mailing list)
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#M001088

Pat
84dc575c33a123789521d53cad0f62ae?d=identicon&s=25 Lloyd Linklater (lloyd)
on 2007-05-07 19:50
Pat Maddox wrote:

> On 5/7/07, arjun ghosh <arjun4ruby@gmail.com> 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?
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2007-05-08 00:22
(Received via mailing list)
On May 7, 2007, at 7:50 PM, Lloyd Linklater 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
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-05-08 00:38
(Received via mailing list)
On May 7, 2007, at 1:50 PM, Lloyd Linklater 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/Cor...
Inflections.html

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
This topic is locked and can not be replied to.