From: "Wayne E. Seguin" Date: 2007-08-21T23:42:43+09:00 Subject: Re: get a class from its name --Apple-Mail-4--1053229991 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed On Aug 20, 2007, at 20:16 , Rolando Abarca wrote: > This is how i'm currently getting a class object from a name: > > cname = "A::B::C::D" > klass = cname.split("::").inject(Kernel) {|a,b| a.const_get > (b)} > > I think it's pretty elegant, but I'm always happy to hear other > ways to do it ;-) > Anyone does this in some other way? (I don't want to eval code...) I use pretty much the same thing: class String def const_get self.split( "::" ).inject( Kernel ) { | parent, child | parent.const_get( child ) } end end # Example module A class B C = 'c' end end >> "A".const_get => A >> "A::B".const_get => A::B >> "A::B::C".const_get => "c" ~Wayne s///g Wayne E. Seguin Sr. Systems Architect & Systems Administrator --Apple-Mail-4--1053229991--