Module: Octiron::Support::Constantize
- Included in:
- Identifiers
- Defined in:
- lib/octiron/support/constantize.rb
Overview
Instance Method Summary collapse
-
#constantize(constant_name) ⇒ Object
Takes a String containing a constant name, and returns the canonical path of the constant (i.e. where it’s defined, even if it’s accessed differently.).
Instance Method Details
#constantize(constant_name) ⇒ Object
Takes a String containing a constant name, and returns the canonical path of the constant (i.e. where it’s defined, even if it’s accessed differently.). If the constant does not exist, a NameError is thrown.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/octiron/support/constantize.rb', line 21 def constantize(constant_name) names = constant_name.split('::') # Trigger a built-in NameError exception including the ill-formed # constant in the message. if names.empty? Object.const_get(constant_name, false) end # Remove the first blank element in case of '::ClassName' notation. if names.size > 1 && names.first.empty? names.shift end # Note: this would be much more complex in Ruby < 1.9.3, so yay for not # bothering to support these! return names.inject(Object) do |constant, name| next constant.const_get(name) end end |