From: Daniel Schierbeck Date: 2006-05-10T21:32:43+09:00 Subject: Re: Intantiating a class by name Daniel Schierbeck wrote: > Isn't it more appropriate to call #to_str on `name'? I.e. > name.to_str.split(... Come to think of it, Symbol doesn't define #to_str, so maybe this is better: name.to_sym.to_str.split(... Furthermore, defining it in Module as a replacement for #const_get makes sense, since they do the same thing, our version is just smarter: class Module alias_method :old_const_get, :const_get def const_get(str) names = str.to_sym.to_s.split('::') names.inject(self){|parent, child| parent.old_const_get(child) } end end class A; class B; class C; end; end; end A.const_get :B => A::B A::B.const_get :C => A::B::C A.const_get "B::C" => A::B::C Kernel.const_get "A::B::C" => A::B::C Cheers, Daniel