Sha256: 8ddc481cccc201e7cf1053ee3913b041dba836f3a76eaf21b47e0a1cd226156e

Contents?: true

Size: 715 Bytes

Versions: 26

Compression:

Stored size: 715 Bytes

Contents

class String

  # Get a constant by a given string name.
  #
  #   "Class".to_const   #=> Class
  #
  # Note this method is not as verstile as it should be,
  # since it can not access contants relative to the current 
  # execution context. But without a binding_of_caller that
  # does not seem possible.
  def to_const
    split('::').inject(Object){ |namespace,name| namespace.const_get(name) }
  end

end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCString < Test::Unit::TestCase

    TestConst = 4

    def test_to_const
      assert_equal( 4, "TCString::TestConst".to_const )
    end

  end

=end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
facets-0.9.0 lib/nano/string/to_const.rb
facets-1.0.0 lib/facet/string/to_const.rb
facets-1.0.3 packages/core/lib/facet/string/to_const.rb
facets-1.2.1 lib/facets/core/string/to_const.rb
facets-1.2.0 lib/facets/core/string/to_const.rb
facets-1.3.0 lib/facets/core/string/to_const.rb
facets-1.1.0 lib/facet/string/to_const.rb
facets-1.3.1 lib/facets/core/string/to_const.rb
facets-1.3.3 lib/facets/core/string/to_const.rb
facets-1.3.2 lib/facets/core/string/to_const.rb
facets-1.4.0 lib/facets/core/string/to_const.rb
facets-1.4.2 lib/facets/core/string/to_const.rb
facets-1.4.1 lib/facets/core/string/to_const.rb
facets-1.4.3 lib/facets/core/string/to_const.rb
facets-1.4.4 lib/facets/core/string/to_const.rb
facets-1.4.5 lib/facets/core/string/to_const.rb
facets-1.7.0 lib/facets/core/string/to_const.rb
facets-1.7.30 lib/facets/core/string/to_const.rb
facets-1.7.38 lib/facets/core/string/to_const.rb
facets-1.7.46 lib/facets/core/string/to_const.rb