# Dumb reflection problem - create object from String

**URL:** <https://www.ruby-forum.com/t/dumb-reflection-problem-create-object-from-string/57325>\
**Category:** Ruby\
**Created:** [April 9, 2006, 8:14pm UTC](https://www.ruby-forum.com/t/dumb-reflection-problem-create-object-from-string/57325 "2006-04-09T20:14:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![parki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b782af/32.png) [@parki](https://www.ruby-forum.com/u/parki)\
**Post date:** [April 9, 2006, 8:14pm UTC](https://www.ruby-forum.com/t/dumb-reflection-problem-create-object-from-string/57325/1 "2006-04-09T20:14:03Z")

</div>

Hello - I’m bashing my head against the wall - I’m sure this is a simple  
question, but damned if I can figure it out.

If I have a String which corresponds to a class name, how do I create an  
instance of this class?

Example:

class MyExampleClass  
…  
end

s = ‘MyExampleClass’

# We want to instantiate a new instance of the class

myExampleClassInstance = ???

I want the behaviour of ‘MyExampleClass.new’ but only using the String  
value of s.

I’m sure it’s a one-liner.

Any help appreciated.

Thanks.

parki…

---

<div class="post-metadata">

**Author:** ![parki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b782af/32.png) [@parki](https://www.ruby-forum.com/u/parki)\
**Post date:** [April 9, 2006, 8:26pm UTC](https://www.ruby-forum.com/t/dumb-reflection-problem-create-object-from-string/57325/2 "2006-04-09T20:26:42Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Brian P. wrote:

> end
> 
> s = ‘MyExampleClass’
> 
> # We want to instantiate a new instance of the class
> 
> myExampleClassInstance = ???
> 
> I want the behaviour of ‘MyExampleClass.new’ but only using the String  
> value of s.

Have a look at Module.const\_get

Module.const\_get(s).new

HTH,  
Sven

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2 (FreeBSD)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iD8DBQFEOVItevlgTHEIT4YRAl3wAJ4j4zJTlcMR9A8ZJKixHXvig0eGywCfUPGN  
W26rak8DMs/szMC+niiVvds=  
=8+ZL  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![parki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b782af/32.png) [@parki](https://www.ruby-forum.com/u/parki)\
**Post date:** [April 9, 2006, 8:29pm UTC](https://www.ruby-forum.com/t/dumb-reflection-problem-create-object-from-string/57325/3 "2006-04-09T20:29:43Z")

</div>

On 4/10/06, Brian P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end

$ irb  
irb(main):001:0\> class K  
irb(main):002:1\> end  
=\> nil  
irb(main):003:0\> k1 = K.new  
=\> #\<K:0xb7fc1788\>  
irb(main):004:0\> k2 = eval(‘K’).new  
=\> #\<K:0xb7fb8094\>  
irb(main):005:0\> k3 = Object.const\_get(‘K’).new  
=\> #\<K:0xb7fb1474\>

---

<div class="post-metadata">

**Author:** ![parki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b782af/32.png) [@parki](https://www.ruby-forum.com/u/parki)\
**Post date:** [April 9, 2006, 8:30pm UTC](https://www.ruby-forum.com/t/dumb-reflection-problem-create-object-from-string/57325/4 "2006-04-09T20:30:17Z")

</div>

Sven K. wrote:

[deletia]

> Module.const\_get(s).new

Thanks!

I knew it would be a one-liner. 🙂

parki…

---

<div class="post-metadata">

**Author:** ![parki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b782af/32.png) [@parki](https://www.ruby-forum.com/u/parki)\
**Post date:** [April 9, 2006, 9:06pm UTC](https://www.ruby-forum.com/t/dumb-reflection-problem-create-object-from-string/57325/5 "2006-04-09T21:06:31Z")

</div>

DÅ?a NedeÄ¾a 9. AprÃ­l 2006 20:26 Gyoung-Yoon N. napÃ­sal:

> > ```
> > ...
> > 
> > ```
> > 
> > I’m sure it’s a one-liner.
> 
> –  
> [http://nohmad.sub-port.net](http://nohmad.sub-port.net)

I would, naively hoping to see YARV released Sometime Soon ™, rather  
use  
, since it probably is / will be easier for YARV to  
work  
with , as far as optimizations are concerned. It also  
makes the intent of the code clearer.

David V.

---

<div class="post-metadata">

**Author:** ![parki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b782af/32.png) [@parki](https://www.ruby-forum.com/u/parki)\
**Post date:** [April 9, 2006, 9:25pm UTC](https://www.ruby-forum.com/t/dumb-reflection-problem-create-object-from-string/57325/6 "2006-04-09T21:25:02Z")

</div>

On 4/9/06, Brian P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> parki…

I am afraid that the only one-liner is eval ☹

suppose s contains the name of your class  
Module.const\_get s does not work unless your class is defined on top  
level.  
In oder to have the _same_ semantics as

eval(s).new

we have to write

m = Module  
s.split(%r{::|.}).each do  
|name|  
m = m.const\_get(name)  
end  
m.new

I suggest to look at ObjectSpace for another alternative, hardly more  
efficent though ☹

Robert

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![parki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b782af/32.png) [@parki](https://www.ruby-forum.com/u/parki)\
**Post date:** [April 9, 2006, 9:43pm UTC](https://www.ruby-forum.com/t/dumb-reflection-problem-create-object-from-string/57325/7 "2006-04-09T21:43:41Z")

</div>

On Apr 9, 2006, at 3:21 PM, Robert D. wrote:

> > I knew it would be a one-liner. 🙂
> 
> - Albert Einstein  
> s.split(/::/).inject(Object) { |p, c| p.const\_get© }.new

There, a one liner (w/o any semi-colons)

---

<div class="post-metadata">

**Author:** ![parki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b782af/32.png) [@parki](https://www.ruby-forum.com/u/parki)\
**Post date:** [April 9, 2006, 9:45pm UTC](https://www.ruby-forum.com/t/dumb-reflection-problem-create-object-from-string/57325/8 "2006-04-09T21:45:49Z")

</div>

Robert D. wrote:

> On 4/9/06, Brian P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > parki…
> 
> I am afraid that the only one-liner is eval ☹
> 
> suppose s contains the name of your class  
> Module.const\_get s does not work unless your class is defined on top  
> level.  
> In oder to have the _same_ semantics as
> 
> eval(s).new
> 
> we have to write
> 
> m = Module  
> s.split(%r{::|.}).each do  
> |name|  
> m = m.const\_get(name)  
> end  
> m.new

s.split(‘::’).inject(Object) { |s,k| s.const\_get(k) }.new

– Jim W.

---

<div class="post-metadata">

**Author:** ![parki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b782af/32.png) [@parki](https://www.ruby-forum.com/u/parki)\
**Post date:** [April 9, 2006, 10:08pm UTC](https://www.ruby-forum.com/t/dumb-reflection-problem-create-object-from-string/57325/9 "2006-04-09T22:08:17Z")

</div>

On 4/9/06, Logan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > Module.const\_get(s).new  
> > > > suppose s contains the name of your class  
> > > > |name|  
> > > > –  
> > > > Yup, very nice  
> > > > What I meant though is that eval seems much simpler and cheaper  
> > > > Let us see  
> > > > yet your’s is fine, just look at the mess when searching through  
> > > > ObjectSpace

cat perf.rb; ruby perf.rb  
require ‘benchmark’

module One  
class Two  
class Three  
end  
end  
end  
s=“One:2️⃣:Three”  
n = 50000  
Benchmark.bm do |x|  
x.report("eval "){ n.times{ eval(s).new } }  
x.report("split ") { n.times{ s.split(/::/).inject(Object) { |p,  
c|  
p.const\_get(c) }.new } }  
x.report(“ObjectSpace”){n.times{x=nil;ObjectSpace.each\_object(Class){|cls|  
x=cls if  
cls.to\_s == s};x.new } }  
end user system total real  
eval 0.280000 0.000000 0.280000 ( 0.372346)  
split 0.540000 0.000000 0.540000 ( 0.621870)  
ObjectSpace 21.920000 0.070000 21.990000 ( 24.452318)

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein
