From: David Vallner Date: 2006-04-10T04:05:33+09:00 Subject: Re: Dumb reflection problem - create object from String --nextPart3036087.AgrSCeo0YQ Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline D=C5=88a Nede=C4=BEa 9. Apr=C3=ADl 2006 20:26 Gyoung-Yoon Noh nap=C3=ADsal: > On 4/10/06, 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 =3D 'MyExampleClass' > > > > # We want to instantiate a new instance of the class > > myExampleClassInstance =3D ??? > > > > 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... > > > > > > -- > > Posted via http://www.ruby-forum.com/. > > $ irb > irb(main):001:0> class K > irb(main):002:1> end > =3D> nil > irb(main):003:0> k1 =3D K.new > =3D> # > irb(main):004:0> k2 =3D eval('K').new > =3D> # > irb(main):005:0> k3 =3D Object.const_get('K').new > =3D> # > > -- > http://nohmad.sub-port.net I would, naively hoping to see YARV released Sometime Soon (tm), rather use= =20 , since it probably is / will be easier for YARV to work= =20 with , as far as optimizations are concerned. It also=20 makes the intent of the code clearer. David Vallner --nextPart3036087.AgrSCeo0YQ Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) iD8DBQBEOVr3+aUhotvmpDgRAgmfAJ42dBZSePKlQeLdzNXasslx//yJRgCdEhRO f4WNJWFvsKYadD+IqQB7qQk= =W/Di -----END PGP SIGNATURE----- --nextPart3036087.AgrSCeo0YQ--