On 7 May 2007, at 16:22, Nanyang Zhan wrote: > 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. 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