From: sishen Date: 2008-02-29T09:35:59+09:00 Subject: Re: String to Class? ------=_Part_43738_24590791.1204245257943 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline cool~ I haven't thought it before. Rails have its owner ext for this function classed Inflector.constantize. And it's implementation is used module_eval. So it can also be : obj = Object.module_eval(foo).new On Fri, Feb 29, 2008 at 1:47 AM, Stefano Crocco wrote: > Alle Thursday 28 February 2008, Aaron Turner ha scritto: > > I want to be able to take a class name stored in a variable as a string > > like: > > > > foo = "Foo::Bar" > > > > and convert that to the class Foo::Bar so that I can call .new(), etc: > > > > obj = foo.new > > > > I know I can just do: > > > > foo = Foo::Bar > > > > but that doesn't solve my problem which is to accept the class name as > > an argument on the command line from the user. > > > > Thanks, > > Aaron > > foo.split('::').inject(Kernel){|res, i| res.const_get(i)} > > Stefano > > > ------=_Part_43738_24590791.1204245257943--