From: Sven Klemm Date: 2006-04-10T03:25:43+09:00 Subject: Re: Dumb reflection problem - create object from String -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Brian Parkinson wrote: > 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. 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 iD8DBQFEOVItevlgTHEIT4YRAl3wAJ4j4zJTlcMR9A8ZJKixHXvig0eGywCfUPGN W26rak8DMs/szMC+niiVvds= =8+ZL -----END PGP SIGNATURE-----