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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs