From: Christophe Grandsire Date: 2005-10-14T04:37:56+09:00 Subject: Re: instance from class name? En r�ponse � ES : > > That works; however, if your class name looks like SomeModule::SomeClass, > you will need to be a bit fancier because const_get only looks for > constants > in the current class or module (the top-level Object instance is the > default): > > class_name.split('::').inject(Object) {|parent, obj| parent.const_get > obj}.new > Oh... my...!!! I thought I understood how inject works, but never would I have guessed that it was capable of such nifty things!!! This is... wow! > #split creates an array ['SomeModule', 'SomeClass'], which we iterate over > using #inject. In the #inject block, the parent will always be the parent > class or module in which we look for the constant and obj is the next class > or module name. The Object is passed in as the initial parent object. the > parent.const_get obj sequence then looks like > > Object.const_get 'SomeModule' => => SomeModule.const_get 'SomeClass' > When you explain it like that it makes perfect sense. But still... wow!!! -- Christophe Grandsire (who's falling in love with Ruby again :) ). http://rainbow.conlang.free.fr You need a straight mind to invent a twisted conlang.