Class: String

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

Instance Method Summary collapse

Instance Method Details

#constantizeObject

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

Blatantly stolen from rails’ activesupport



15
16
17
18
19
20
21
22
# File 'lib/stellr/utils.rb', line 15

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