# Creating instance of an object without knowing it's name

**URL:** <https://www.ruby-forum.com/t/creating-instance-of-an-object-without-knowing-its-name/156355>\
**Category:** Rails\
**Created:** [December 25, 2008, 1:32pm UTC](https://www.ruby-forum.com/t/creating-instance-of-an-object-without-knowing-its-name/156355 "2008-12-25T13:32:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![astropanic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/958977/32.png) [@astropanic](https://www.ruby-forum.com/u/astropanic)\
**Post date:** [December 25, 2008, 1:32pm UTC](https://www.ruby-forum.com/t/creating-instance-of-an-object-without-knowing-its-name/156355/1 "2008-12-25T13:32:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![astropanic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/958977/32.png) [@astropanic](https://www.ruby-forum.com/u/astropanic)\
**Post date:** [December 25, 2008, 2:06pm UTC](https://www.ruby-forum.com/t/creating-instance-of-an-object-without-knowing-its-name/156355/2 "2008-12-25T14:06:21Z")

</div>

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](http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html#M001058)

Fred
