Passing by value of object of a model


#1

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


#2

On 5/7/07, arjun ghosh removed_email_address@domain.invalid wrote:

“food_dept”.classify.constantize => FoodDept

Pat


#3

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.


#4

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


#5

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


#6

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.


#7

On 5/7/07, arjun ghosh 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 :slight_smile: 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 :slight_smile: but you also shouldn’t be
embarrassed to ask a question.

Pat


#8

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…:slight_smile:
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


#9

On 5/7/07, arjun ghosh 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 :slight_smile: ), 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


#10

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


#11

Pat M. wrote:

On 5/7/07, arjun ghosh 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?


#12

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


#13

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/
Inflections.html

Rob B. http://agileconsultingllc.com
removed_email_address@domain.invalid