Hi --

On Tue, 21 Aug 2007, Tim Pease wrote:

> On 8/20/07, David A. Black <dblack / rubypal.com> wrote:
>> Hi --
>>
>> On Tue, 21 Aug 2007, 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 think it's usually done the way you've got it there. It's one of
>> those things that people write over and over (like map_with_index and
>> singleton_method), hopefully some day to enter the core as
>> const_get_deep or something.
>>
>
> "A::B::C::D".to_class
>
> It could just be a wrapper for the core method rb_path2class -- which
> does the same thing in Ruby C API land.

It's a more general-purpose thing, though; it's for any constant, not
just classes. It's really just a variation on const_get, which already
will give you any constant from any existing class/module nesting.


David

-- 
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
   RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
     & consulting:  Ruby Power and Light, LLC (http://www.rubypal.com)