Sha256: 7f4aea8c0be36733910bfa0939150003dbe49551e19aeff654e5823eb2c0cf1d

Contents?: true

Size: 1.22 KB

Versions: 7

Compression:

Stored size: 1.22 KB

Contents

#--
# Author::    Tyler Rick
# Copyright:: Copyright (c) 2007 QualitySmith, Inc.
# License::   Ruby License
# Submit to Facets?:: Yes!
# Developer notes::
#++

require 'rubygems'
require 'facets/core/symbol/to_proc'

class Module
  # Very similar to Facets' +Module#nesting+, but, whereas +nesting+ will break <tt>A::B</tt> into an array of _constants_ represting nesting
  # (<tt>[A, A::B]</tt>), this method will split it into an array of _symbols_: <tt>[:A, :B]</tt>.
  #
  # Note that the second element in this array, <tt>:B</tt>, is _not_ fully qualified, so you could not do a <tt>const_get</tt>
  # on that symbol.
  def split
    name.split(/::/).map(&:to_sym)
  end

  # Like Module#split, only this operates on a string/symbol. Useful for when you don't want to or can't actually instantiate
  # the module represented by the symbol.
  def self.split_name(name)
    name.to_s.split(/::/).map(&:to_sym)
  end
end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test
require 'test/unit'

module A
  module B
  end
end

class TheTest < Test::Unit::TestCase
  def test_A
    assert_equal [:A], A.split
  end
  def test_A_B
    assert_equal [:A, :B], A::B.split
  end

end
=end


Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
quality_extensions-0.1.2 lib/quality_extensions/module/split.rb
quality_extensions-0.1.1 lib/qualitysmith_extensions/module/split.rb
quality_extensions-0.1.4 lib/quality_extensions/module/split.rb
qualitysmith_extensions-0.0.63 lib/qualitysmith_extensions/module/split.rb
qualitysmith_extensions-0.0.60 lib/qualitysmith_extensions/module/split.rb
qualitysmith_extensions-0.0.64 lib/qualitysmith_extensions/module/split.rb
qualitysmith_extensions-0.0.49 lib/qualitysmith_extensions/module/split.rb