Forum: Ruby on Rails creating instance of an object without knowing it's name

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A391f932c5bb61131c99f8543a07f697?d=identicon&s=25 astropanic (Guest)
on 2008-12-25 13:32
(Received via mailing list)
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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-25 14:06
(Received via mailing list)
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/C...

Fred
This topic is locked and can not be replied to.