Sha256: a88d22715ed5fcc89e306563b605441f0656ab762d396be4f1362f69cfbcdc10

Contents?: true

Size: 776 Bytes

Versions: 10

Compression:

Stored size: 776 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
  #
  #   require 'test/unit'
  #   Test.constant("Unit::Assertions")   # -> Test::Unit::Assertions
  #   Test.constant("::Test::Unit")       # -> Test::Unit
  #
  #   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

10 entries across 10 versions & 2 rubygems

Version Path
facets-2.2.1 lib/core/facets/kernel/constant.rb
facets-2.3.0 lib/core/facets/kernel/constant.rb
facets-2.4.0 lib/facets/kernel/constant.rb
facets-2.2.0 lib/core/facets/kernel/constant.rb
facets-2.4.1 lib/facets/kernel/constant.rb
facets-2.4.3 lib/core/facets/kernel/constant.rb
facets-2.4.4 lib/core/facets/kernel/constant.rb
facets-2.4.2 lib/core/facets/kernel/constant.rb
facets-2.4.5 lib/core/facets/kernel/constant.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/kernel/constant.rb