If you need something simple that only handles your special case, you can write
Object.const_get("Admin").const_get("MetaDatasController")
But if you want something more general, split the line into :: and resolve the names one by one:
def class_from_string(str) str.split('::').inject(Object) do |mod, class_name| mod.const_get(class_name) end end the_class = class_from_string("Admin::MetaDatasController")
At the first iteration of Object the Admin constant is requested and returns the module or Admin class, and then at the second iteration, the MetaDatasController constant is set to this module or class and returns this class, since there are no more components returned by the class from the method (if there were more components, they would be repeated until they find the latter).
Theo Jul 02 '10 at 6:38 2010-07-02 06:38
source share