astropanic
2008-Dec-25 12:31 UTC
creating instance of an object without knowing it''s name
Hi, I have a variable @foo that holds the name of an object. How I can create a instance of that object. An example: I need to create a instance of an "Elephant" object @object_name = "Elephant" @object_name.new # this does''nt work Is there a way to do this without eval ? Regards --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Dec-25 13:05 UTC
Re: creating instance of an object without knowing it''s name
On 25 Dec 2008, at 12:31, astropanic wrote:> > Hi, > I have a variable @foo that holds the name of an object. > How I can create a instance of that object. > > An example: > > I need to create a instance of an "Elephant" object > > @object_name = "Elephant" > @object_name.new # this does''nt work > > Is there a way to do this without eval ? >constantize http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html#M001058 Fred> Regards > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---