# Dynamic object creation

**URL:** <https://www.ruby-forum.com/t/dynamic-object-creation/168777>\
**Category:** Ruby\
**Created:** [May 25, 2009, 6:58pm UTC](https://www.ruby-forum.com/t/dynamic-object-creation/168777 "2009-05-25T18:58:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ken\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f17d59/32.png) [@Ken\_T](https://www.ruby-forum.com/u/Ken_T)\
**Post date:** [May 25, 2009, 6:58pm UTC](https://www.ruby-forum.com/t/dynamic-object-creation/168777/1 "2009-05-25T18:58:28Z")

</div>

sorry if i’ve missed something obvious

is there a way in ruby to dynamically create an object from a variable  
that holds the object’s name?

something that does object.create(“some\_object\_name”)

---

<div class="post-metadata">

**Author:** ![Ken\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f17d59/32.png) [@Ken\_T](https://www.ruby-forum.com/u/Ken_T)\
**Post date:** [May 25, 2009, 7:23pm UTC](https://www.ruby-forum.com/t/dynamic-object-creation/168777/2 "2009-05-25T19:23:26Z")

</div>

On Mon, May 25, 2009 at 5:58 PM, Ken T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> sorry if i’ve missed something obvious
> 
> is there a way in ruby to dynamically create an object from a variable  
> that holds the object’s name?
> 
> something that does object.create(“some\_object\_name”)

Try:

Kernel.const\_get(“some\_object\_name”)

---

<div class="post-metadata">

**Author:** ![Ken\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f17d59/32.png) [@Ken\_T](https://www.ruby-forum.com/u/Ken_T)\
**Post date:** [May 25, 2009, 7:21pm UTC](https://www.ruby-forum.com/t/dynamic-object-creation/168777/3 "2009-05-25T19:21:29Z")

</div>

What’s the difference between that and

somename = ObjectClass.new

---

<div class="post-metadata">

**Author:** ![Ken\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f17d59/32.png) [@Ken\_T](https://www.ruby-forum.com/u/Ken_T)\
**Post date:** [May 25, 2009, 7:24pm UTC](https://www.ruby-forum.com/t/dynamic-object-creation/168777/4 "2009-05-25T19:24:37Z")

</div>

Ken T. wrote:

> sorry if i’ve missed something obvious
> 
> is there a way in ruby to dynamically create an object from a variable  
> that holds the object’s name?
> 
> something that does object.create(“some\_object\_name”)

Assuming you mean “a variable that holds the class’s name”, check out  
Module.const\_get. This method takes a string and returns the value of  
the constant having that name. Since the value of the constant Array is  
the Array class object, we can get the Array class object like this:

cls = Module.const\_get(“Array”)

And then call new on the class object to create a new array:

ary = cls.new

---

<div class="post-metadata">

**Author:** ![Ken\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f17d59/32.png) [@Ken\_T](https://www.ruby-forum.com/u/Ken_T)\
**Post date:** [May 25, 2009, 7:26pm UTC](https://www.ruby-forum.com/t/dynamic-object-creation/168777/5 "2009-05-25T19:26:42Z")

</div>

Ken T. wrote:

> sorry if i’ve missed something obvious
> 
> is there a way in ruby to dynamically create an object from a variable  
> that holds the object’s name?

That doesn’t make sense - if the object has a name then surely it  
already exists?

Perhaps you wish to create an object where a variable holds the name of  
the _class_ you wish to create a new instance of. In that case:

klassname = “String”  
klass = Object.const\_get(klassname)  
foo = klass.new

If the class name contains :: scope separator, then you need to split on  
that first.

klass = klassname.split(’::’).inject(Object) { |base,item|  
base.const\_get(item)  
}

---

<div class="post-metadata">

**Author:** ![Ken\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f17d59/32.png) [@Ken\_T](https://www.ruby-forum.com/u/Ken_T)\
**Post date:** [May 25, 2009, 9:47pm UTC](https://www.ruby-forum.com/t/dynamic-object-creation/168777/6 "2009-05-25T21:47:20Z")

</div>

On 25.05.2009 19:37, Ken T. wrote:

> thanks for all the responses
> 
> i’m just learning ruby so sometimes i don’t know which end of the  
> screwdriver to grab

Hint: if it hurts you got the wrong end. 😉

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Ken\_T](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f17d59/32.png) [@Ken\_T](https://www.ruby-forum.com/u/Ken_T)\
**Post date:** [May 25, 2009, 7:37pm UTC](https://www.ruby-forum.com/t/dynamic-object-creation/168777/7 "2009-05-25T19:37:50Z")

</div>

thanks for all the responses

i’m just learning ruby so sometimes i don’t know which end of the  
screwdriver to grab
