Method: Extlib::Inflection.constantize

Defined in:
lib/extlib/inflection.rb

.constantize(camel_cased_word) ⇒ Object

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


98
99
100
101
102
103
104
# File 'lib/extlib/inflection.rb', line 98

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

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