Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/ki/utils/extra_ruby.rb

Instance Method Summary collapse

Instance Method Details

#to_classObject

Converts a string to a class

Examples

class User
end

"user".to_class == User


11
12
13
14
15
16
17
18
19
20
# File 'lib/ki/utils/extra_ruby.rb', line 11

def to_class
  chain = split '::'
  klass = Kernel
  chain.each do |klass_string|
    klass = klass.const_get klass_string.split('_').map(&:capitalize).join('')
  end
  klass.is_a?(Class) ? klass : nil
rescue NameError
  nil
end