Method: Kernel#constant

Defined in:
lib/core/facets/kernel/constant.rb

#constant(const) ⇒ Object

This is similar to Module#const_get but is accessible at all levels, and, unlike const_get, can handle module hierarchy.

constant("Fixnum")                  # => Fixnum
constant(:Fixnum)                   # => Fixnum

constant("Process::Sys")            # => Process::Sys
constant("Regexp::MULTILINE")       # => 4

TODO: As of Ruby 2.0, #constant can be deprecated, or aliased to #get_const.

CREDIT: Trans



16
17
18
19
20
# File 'lib/core/facets/kernel/constant.rb', line 16

def constant(const)
  const = const.to_s.dup
  base = const.sub!(/^::/, '') ? Object : ( self.kind_of?(Module) ? self : self.class )
  const.split(/::/).inject(base){ |mod, name| mod.const_get(name) }
end