Sha256: 66c4a897447333e2d6d4c5172c6848cca8e2d78402aaffaa7c0e6716243fe44f

Contents?: true

Size: 1.15 KB

Versions: 21

Compression:

Stored size: 1.15 KB

Contents

#          Copyright (c) 2008 Michael Fellinger m.fellinger@gmail.com
# All files in this distribution are subject to the terms of the Ruby license.

module Ramaze
  module CoreExtensions

    # Extensions for Kernel

    module Object

      # Original from Trans (Facets 1.4.5)
      # 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

      def constant(const)
        const = const.to_s.dup

        if const.sub!(/^::/, '')
          base = Object
        elsif self.kind_of?(Module)
          base = self
        else
          base = self.class
        end

        const.split(/::/).inject(base){ |mod, name| mod.const_get(name) }
      end
    end

  end
end

Version data entries

21 entries across 21 versions & 4 rubygems

Version Path
Pistos-ramaze-2008.09 lib/ramaze/snippets/kernel/constant.rb
Pistos-ramaze-2008.12 lib/ramaze/snippets/kernel/constant.rb
Pistos-ramaze-2009.01 lib/ramaze/snippets/kernel/constant.rb
Pistos-ramaze-2009.02 lib/ramaze/snippets/kernel/constant.rb
Pistos-ramaze-2009.04.08 lib/ramaze/snippets/kernel/constant.rb
manveru-ramaze-2008.09 lib/ramaze/snippets/kernel/constant.rb
manveru-ramaze-2008.10 lib/ramaze/snippets/kernel/constant.rb
manveru-ramaze-2008.12 lib/ramaze/snippets/kernel/constant.rb
manveru-ramaze-2009.01 lib/ramaze/snippets/kernel/constant.rb
manveru-ramaze-2009.04.01 lib/ramaze/snippets/kernel/constant.rb
manveru-ramaze-2009.04.08 lib/ramaze/snippets/kernel/constant.rb
manveru-ramaze-2009.04.18 lib/ramaze/snippets/kernel/constant.rb
manveru-ramaze-2009.04.22 lib/ramaze/snippets/kernel/constant.rb
manveru-ramaze-2009.04 lib/ramaze/snippets/kernel/constant.rb
ptomato-ramaze-2009.02.1 lib/ramaze/snippets/kernel/constant.rb
ptomato-ramaze-2009.02 lib/ramaze/snippets/kernel/constant.rb
ramaze-2009.01 lib/ramaze/snippets/kernel/constant.rb
ramaze-2008.11 lib/ramaze/snippets/kernel/constant.rb
ramaze-2009.03 lib/ramaze/snippets/kernel/constant.rb
ramaze-2009.04 lib/ramaze/snippets/kernel/constant.rb