[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: [Rails] passing by value of object of a model

David Chelimsky

5/7/2007 12:10:00 PM

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

>i think there should be some better elegant
> manner of doing this. can any one help me with this?
> ciao
> -AG
>
> --~--~---------~--~----~------------~-------~--~----~
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to
> rubyonrails-talk@googlegroups.com
> To unsubscribe from this group, send email to
> rubyonrails-talk-unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-...
> -~----------~----~----~----~------~----~------~--~---
>
>

1 Answer

Pat Maddox

5/7/2007 5:36:00 PM

0

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.ht...
http://api.rubyonrails.com/classes/Inflector.ht...

Pat

On 5/7/07, arjun ghosh <arjun4ruby@gmail.com> wrote:
> 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
>
>
> On 5/7/07, David Chelimsky <dchelimsky@gmail.com> wrote:
> > 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
> >
> > >i think there should be some better elegant
> > > manner of doing this. can any one help me with this?
> > > ciao
> > > -AG
> > >
> > > --~--~---------~--~----~------------~-------~--~----~
> > > You received this message because you are subscribed to the Google
> Groups
> > > "Ruby on Rails: Talk" group.
> > > To post to this group, send email to
> > > rubyonrails-talk@googlegroups.com
> > > To unsubscribe from this group, send email to
> > > rubyonrails-talk-unsubscribe@googlegroups.com
> > > For more options, visit this group at
> > > http://groups.google.com/group/rubyonrails-...
> > > -~----------~----~----~----~------~----~------~--~---
> > >
> > >
> >
> >
>
>