From: Gordon Thiesfeld Date: 2008-02-29T02:42:11+09:00 Subject: Re: String to Class? On Thu, Feb 28, 2008 at 11:28 AM, Aaron Turner wrote: > 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 > > -- > Aaron Turner > http://synfin.net/ > http://tcpreplay.synfin.net/ - Pcap editing & replay tools for Unix > They that can give up essential liberty to obtain a little temporary > safety deserve neither liberty nor safety. -- Benjamin Franklin > > I'm sure there's a better way, but: module Foo class Bar end end foo = "Foo::Bar" foo = foo.split("::").inject(Object){|s,v| s.const_get(v) } obj = foo.new p obj