dblack / wobblini.net wrote:

> 
>   class Object
>     def const_get_recursive(const)
>       const.split("::").inject(Object) {|c1,c2| c1.const_get(c2)}
>     end
>   end
> 
>   const_get_recursive("MM_#{s}::MC::#{s}").new
> 
> 
> David
> 

I know that eval is not save but it appears that it is much faster than 
inject version

class A
     class B
        C = "foo"
     end
end


n=10000
Benchmark.bm do |x|
     x.report("eval") { n.times{ eval("A::B::C") }  }
     x.report("inject") { n.times{ Object.const_get_recursive("A::B::C") 
}  }
end

        user     system      total        real
eval   0.000000   0.000000   0.000000 (  1.250000)
inject 0.000000   0.000000   0.000000 (  2.656000)
 > Execution finished.


maybe eval does some caching or other kind of optimizations or maybe 
because it's just c...?

lopex