Creating instance of an object without knowing it's name


#1

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


#2

On 25 Dec 2008, at 12:31, astropanic wrote:

@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