KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
The following example fails class A class B end end p Object.const_get 'A' # => A p Object.const_get 'A::B' # => NameError: wrong constant name A::B UPDATE Questions about the topic asked earlier: Cast between String and Classname Ruby String#to_class Get a class by name in Ruby? The last one gives a nice solution which can be evolved into class String def to_class self.split('::').inject(Object) do |mod, class_name| mod.const_get(class_name) end end end class A class B end end p "A::B".to_class # => A::B
Tags (comma-separated)
Save Edits
Cancel