Hi, i also program in php and one of the concepts i miss in ruby is the "variable variable" $a="test" $$a="test2" echo $test will output "test2" this can be usefull when a field on a form determines which object to create example: $objecttocreate="car" $obj=new $$objecttocreate will create a new car object is this possible in ruby? i can''t figure it out -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
x = ''MyModel'' obj = Module.const_get(x).new obj.class => MyModel On 2/28/07, newbee <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi, i also program in php and one of the concepts i miss in ruby is the > "variable variable" > > $a="test" > $$a="test2" > > echo $test will output "test2" > > this can be usefull when a field on a form determines which object to > create > example: > > $objecttocreate="car" > > $obj=new $$objecttocreate will create a new car object > > is this possible in ruby? > i can''t figure it out > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''m new to Ruby and Rails, too. Chris'' example is very cool - I hadn''t seen "Module.const_get" before. A less sophisticated (and much more dangerous) way to do the same thing would be to use eval: x = "Foo" obj = eval("#{x}.new") obj.class => Foo Actually, good Rubyists can manage to construct whole classes (and modules and definitions) on the fly, not just objects. Cool stuff. Jim On Feb 28, 6:40 am, newbee <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, i also program in php and one of the concepts i miss in ruby is the > "variable variable" > > $a="test" > $$a="test2" > > echo $test will output "test2" > > this can be usefull when a field on a form determines which object to > create > example: > > $objecttocreate="car" > > $obj=new $$objecttocreate will create a new car object > > is this possible in ruby? > i can''t figure it out > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---