Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/skynet/skynet_ruby_extensions.rb

Instance Method Summary collapse

Instance Method Details

#constantizeObject

THIS IS TAKEN DIRECTLY FROM ActiveSupport::Inflector Constantize tries to find a declared constant with the name specified in the string. It raises a NameError when the name is not in CamelCase or is not initialized.

Examples

"Module".constantize #=> Module
"Class".constantize #=> Class


46
47
48
49
50
51
52
# File 'lib/skynet/skynet_ruby_extensions.rb', line 46

def constantize
  unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ self
    raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
  end

  Object.module_eval("::#{$1}", __FILE__, __LINE__)
end