From: Logan Capaldo Date: 2006-04-10T04:41:44+09:00 Subject: Re: Dumb reflection problem - create object from String On Apr 9, 2006, at 3:21 PM, Robert Dober wrote: > On 4/9/06, Brian Parkinson wrote: >> >> Sven Klemm wrote: >> >> [deletia] >> >>> Module.const_get(s).new >> >> Thanks! >> >> I knew it would be a one-liner. :-) >> >> 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 s.split(/::/).inject(Object) { |p, c| p.const_get(c) }.new There, a one liner (w/o any semi-colons)