Get a class by name in Ruby?

Ruby

Ruby Problem Overview


Having a string with the module and name of a class, like:

"Admin::MetaDatasController"

how do I get the actual class?

The following code works if there's no module:

Kernel.const_get("MetaDatasController")

but it breaks with the module:

ruby-1.8.7-p174 > Kernel.const_get("Admin::MetaDatasController")
NameError: wrong constant name Admin::MetaDatasController
        from (irb):34:in `const_get'
        from (irb):34
ruby-1.8.7-p174 > 

Ruby Solutions


Solution 1 - Ruby

If you want something simple that handles just your special case you can write

Object.const_get("Admin").const_get("MetaDatasController")

But if you want something more general, split the string on :: and resolve the names one after the other:

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")

On the first iteration Object is asked for the constant Admin and returns the Admin module or class, then on the second iteration that module or class is asked for the constant MetaDatasController, and returns that class. Since there are no more components that class is returned from the method (if there had been more components it would have iterated until it found the last).

Solution 2 - Ruby

ActiveSupport provides a method called constantize, which will do this. If you are on Rails, which I assume you are based on the name of your constant, then you already have ActiveSupport loaded.

require 'active_support/core_ext/string'

class Admin
  class MetaDatasController
  end
end

"Admin::MetaDatasController".constantize # => Admin::MetaDatasController

To see how the method is implemented, check out https://github.com/rails/rails/blob/85c2141fe3d7edb636a0b5e1d203f05c70db39dc/activesupport/lib/active_support/inflector/methods.rb#L230-L253

Solution 3 - Ruby

In Ruby 2.x, you can just do this:

Object.const_get('Admin::MetaDatasController')
=> Admin::MetaDatasController

Solution 4 - Ruby

i could be way off-base, but wouldn't eval return the class?

eval("Admin::MetaDatasController")

so eval("Admin::MetaDatasController").new would be the same as Admin::MetaDatasController.new

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpupenoView Question on Stackoverflow
Solution 1 - RubyTheoView Answer on Stackoverflow
Solution 2 - RubyJoshua CheekView Answer on Stackoverflow
Solution 3 - RubyhololeapView Answer on Stackoverflow
Solution 4 - RubypotatopeelingsView Answer on Stackoverflow