# Dynamic object creation in ruby

**URL:** <https://www.ruby-forum.com/t/dynamic-object-creation-in-ruby/164165>\
**Category:** Ruby\
**Created:** [March 29, 2009, 5:30am UTC](https://www.ruby-forum.com/t/dynamic-object-creation-in-ruby/164165 "2009-03-29T05:30:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![clashfan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@clashfan](https://www.ruby-forum.com/u/clashfan)\
**Post date:** [March 29, 2009, 5:30am UTC](https://www.ruby-forum.com/t/dynamic-object-creation-in-ruby/164165/1 "2009-03-29T05:30:07Z")

</div>

I’m trying to do dynamic object creation in ruby.

This works:

john = eval(“Master::Person”).new(“John”)

This fails:

frank = Object.const\_get(“Master::Person”).new(“Frank”)

Does anyone know why the second way fails?

---

<div class="post-metadata">

**Author:** ![clashfan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@clashfan](https://www.ruby-forum.com/u/clashfan)\
**Post date:** [March 29, 2009, 6:49am UTC](https://www.ruby-forum.com/t/dynamic-object-creation-in-ruby/164165/2 "2009-03-29T06:49:40Z")

</div>

Clash F. wrote:

> This works:
> 
> john = eval(“Master::Person”).new(“John”)
> 
> This fails:
> 
> frank = Object.const\_get(“Master::Person”).new(“Frank”)
> 
> Does anyone know why the second way fails?

Because there is no constant named Master::Person. There’s a constant  
named Person inside the constant named Master. This is going to make a  
lot more sense by example:

Object.const\_get(‘Master’).const\_get(‘Person’).new(‘Frank’)

For what it’s worth, unless there’s a reason to use strings, you may as  
well use symbols there:

Object.const\_get(:Master).const\_get(:Person).new(‘Frank’)

---

<div class="post-metadata">

**Author:** ![clashfan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@clashfan](https://www.ruby-forum.com/u/clashfan)\
**Post date:** [March 29, 2009, 6:54am UTC](https://www.ruby-forum.com/t/dynamic-object-creation-in-ruby/164165/3 "2009-03-29T06:54:56Z")

</div>

On Sun, Mar 29, 2009 at 9:00 AM, Clash F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## Does anyone know why the second way fails?
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

This will work:  
Object.const\_get(“Master”)::Person.new(“Frank”)

Or:  
Object.const\_get(“Master”).const\_get(“Person”).new(“Frank”)

Or:  
“Master::Person”.split(‘::’).reduce(Object){|cls, c| cls.const\_get(c) }

But your question was ‘why?’… I guess the short answer is #const\_get  
just doesn’t interpret the scope operator, ‘::’. Actually, thinking  
of it as an operator (hence it works in eval) and not a naming  
convention is perhaps helpful.

cheers,  
lasitha

---

<div class="post-metadata">

**Author:** ![clashfan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@clashfan](https://www.ruby-forum.com/u/clashfan)\
**Post date:** [March 29, 2009, 1:39pm UTC](https://www.ruby-forum.com/t/dynamic-object-creation-in-ruby/164165/4 "2009-03-29T13:39:45Z")

</div>

> frank = Object.const\_get(“Master::Person”).new(“Frank”)
> 
> Does anyone know why the second way fails?

frank = Master.const\_get(“Person”).new(“Frank”)

---

<div class="post-metadata">

**Author:** ![clashfan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@clashfan](https://www.ruby-forum.com/u/clashfan)\
**Post date:** [March 29, 2009, 3:55pm UTC](https://www.ruby-forum.com/t/dynamic-object-creation-in-ruby/164165/5 "2009-03-29T15:55:44Z")

</div>

On 29.03.2009 13:40, Brian C. wrote:

> > frank = Object.const\_get(“Master::Person”).new(“Frank”)
> > 
> > Does anyone know why the second way fails?
> 
> frank = Master.const\_get(“Person”).new(“Frank”)

Erm, that’s a hybrid solution. If you know the names beforehand, you  
can just do

frank = Master::Person.new “Frank”

I would assume that the OP receives the full qualified name as a String  
and wants to get the class from there which can only be achieved with  
any of the other solutions shown.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![clashfan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@clashfan](https://www.ruby-forum.com/u/clashfan)\
**Post date:** [March 29, 2009, 4:40pm UTC](https://www.ruby-forum.com/t/dynamic-object-creation-in-ruby/164165/6 "2009-03-29T16:40:51Z")

</div>

Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> On 29.03.2009 13:40, Brian C. wrote:
> 
> > > frank = Object.const\_get(“Master::Person”).new(“Frank”)
> > > 
> > > Does anyone know why the second way fails?  
> > > frank = Master.const\_get(“Person”).new(“Frank”)
> 
> Erm, that’s a hybrid solution. If you know the names beforehand, you  
> can just do
> 
> frank = Master::Person.new “Frank”

Then the answer for the OP would be:

frank = Object.const\_get(“Master”).const\_get(“Person”).new(“Frank”)

const\_get takes only a simple constant name, not a qualified name.

irb(main):247:0\> (module Master  
(class Person  
(def initialize(name)  
@name=name  
end)  
end)  
end)  
nil  
irb(main):254:0\> frank =  
Object.const\_get(“Master”).const\_get(“Person”).new(“Frank”)  
#\<Master:🧑0x3465ac @name=“Frank”\>

> I would assume that the OP receives the full qualified name as a  
> String and wants to get the class from there which can only be  
> achieved with any of the other solutions shown.

Obviously, he wants to replace the literals by variables.

---

<div class="post-metadata">

**Author:** ![clashfan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@clashfan](https://www.ruby-forum.com/u/clashfan)\
**Post date:** [March 29, 2009, 5:55pm UTC](https://www.ruby-forum.com/t/dynamic-object-creation-in-ruby/164165/7 "2009-03-29T17:55:54Z")

</div>

On 29.03.2009 16:38, Pascal J. Bourguignon wrote:

> > frank = Master::Person.new “Frank”
> 
> Then the answer for the OP would be:
> 
> frank = Object.const\_get(“Master”).const\_get(“Person”).new(“Frank”)

Why would anyone want to use Strings when he knows the names (and  
consequently their number) beforehand? That does not make sense.

> const\_get takes only a simple constant name, not a qualified name.

Yes, and that’s why the solutions that were provided use some form of  
String#split to cut a fully qualified name down to individual constants.

> > I would assume that the OP receives the full qualified name as a  
> > String and wants to get the class from there which can only be  
> > achieved with any of the other solutions shown.
> 
> Obviously, he wants to replace the literals by variables.

Apart from the plurals:

> This works:
> 
> john = eval(“Master::Person”).new(“John”)
> 
> This fails:
> 
> frank = Object.const\_get(“Master::Person”).new(“Frank”)

Regards

robert
