From: "David A. Black" Date: 2009-01-24T10:41:12+09:00 Subject: Re: instantiate a class dynamically --1926193751-1457453863-1232761384=:8101 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-1457453863-1232761384=:8101" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --1926193751-1457453863-1232761384=:8101 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Sat, 24 Jan 2009, F. Senault wrote: > Le 24 janvier 2009 =E0 02:12, rpardee@gmail.com a =E9crit : > >> Hey All, >> >> If I have the name of a class in a string var, is it possible to get a >> new instance of that class (and if so, how)? The following do *not* >> work: >> >> my_string =3D "Hash" >> >> my_object =3D Class(my_string) >> my_object =3D Class.new(my_string) >> my_object =3D Object.new(my_string) > > my_object =3D Object.const_get(my_string.to_sym).new > > (The to_sym is mandatory in ruby 1.9.) I don't think so: $ ruby19 -ve 'p Object.const_get("String")' ruby 1.9.1 (2008-12-30 patchlevel-0 revision 21203) [i386-darwin9.5.0] String David --=20 David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2) http://www.wishsight.com =3D> Independent, social wishlist management! --1926193751-1457453863-1232761384=:8101-- --1926193751-1457453863-1232761384=:8101--