# Create an object from a string?

**URL:** <https://www.ruby-forum.com/t/create-an-object-from-a-string/52813>\
**Category:** Rails\
**Created:** [January 19, 2006, 11:27am UTC](https://www.ruby-forum.com/t/create-an-object-from-a-string/52813 "2006-01-19T11:27:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_McGrath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8e8cbc/32.png) [@John\_McGrath](https://www.ruby-forum.com/u/John_McGrath)\
**Post date:** [January 19, 2006, 11:27am UTC](https://www.ruby-forum.com/t/create-an-object-from-a-string/52813/1 "2006-01-19T11:27:57Z")

</div>

I’m trying to dynamically create an instance of an object at runtime,  
from a  
String. I have a method that returns one of a number of Strings (“Car”,  
“Motorcycle”, “Bicycle”), and when I receive the String, I then want to  
instantiate one of those objects.

Seems like there should be a way to do it in Ruby, but maybe I’ve been  
up  
too long – can’t find it. In Java, I’d be doing something like this:

Class.forName(“Car”).newInstance();

Any help greatly appreciated.

John

---

<div class="post-metadata">

**Author:** ![John\_McGrath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8e8cbc/32.png) [@John\_McGrath](https://www.ruby-forum.com/u/John_McGrath)\
**Post date:** [January 19, 2006, 11:33am UTC](https://www.ruby-forum.com/t/create-an-object-from-a-string/52813/2 "2006-01-19T11:33:59Z")

</div>

2006/1/19, John McGrath [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Class.forName(“Car”).newInstance();

(eval “Time”).new

---

<div class="post-metadata">

**Author:** ![John\_McGrath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8e8cbc/32.png) [@John\_McGrath](https://www.ruby-forum.com/u/John_McGrath)\
**Post date:** [January 19, 2006, 11:33am UTC](https://www.ruby-forum.com/t/create-an-object-from-a-string/52813/3 "2006-01-19T11:33:59Z")

</div>

John McGrath wrote:

> Any help greatly appreciated.

If you’re classes are in the top level namespace, you can do…  
Object.const\_get( ‘Car’ ).new

If you’re classes are in another namespace and you know that namespace,  
you can do the same.

module A  
module B  
class C  
end  
end  
end

c = A::B.const\_get( ‘C’ ).new

Zach

---

<div class="post-metadata">

**Author:** ![John\_McGrath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8e8cbc/32.png) [@John\_McGrath](https://www.ruby-forum.com/u/John_McGrath)\
**Post date:** [January 19, 2006, 6:30pm UTC](https://www.ruby-forum.com/t/create-an-object-from-a-string/52813/4 "2006-01-19T18:30:36Z")

</div>

On 1/19/06, John McGrath [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Class.forName(“Car”).newInstance();  
> The other examples so far will work, but in Rails you can simply do:  
> instance = string\_containing\_class\_name.constantize.new

e.g.  
class\_name = “Motorcycle”  
@motorcycle = class\_name.constantize.new

…and now, @motorcycle is an instance of Motorcycle.

---

<div class="post-metadata">

**Author:** ![John\_McGrath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8e8cbc/32.png) [@John\_McGrath](https://www.ruby-forum.com/u/John_McGrath)\
**Post date:** [January 19, 2006, 10:26pm UTC](https://www.ruby-forum.com/t/create-an-object-from-a-string/52813/5 "2006-01-19T22:26:21Z")

</div>

Take a look at YAML. It makes it trivial to make a whole object graph  
from a string.
