Sha256: e62aaac75d75d25ddfafb5ce7ccd194b64d604c6934bb23e2e1cc15206542fb4

Contents?: true

Size: 701 Bytes

Versions: 2

Compression:

Stored size: 701 Bytes

Contents

module Kernel

  # 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

  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

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
facets-3.1.0 lib/core/facets/kernel/constant.rb
facets-3.0.0 lib/core/facets/kernel/constant.rb