Sha256: be0675b4f251434757fe7b6ea4113ee33103b9ce83c0c0eac4db2da13566006a

Contents?: true

Size: 1004 Bytes

Versions: 10

Compression:

Stored size: 1004 Bytes

Contents

# TITLE:
#
#   Symbol Generation
#
# DESCRIPTION:
#
#    Symbol generation extensions.
#
# AUTHORS:
#
#   CREDIT Thomas Sawyer
#
# NOTES:
#
#   TODO Is Symbol#chomp worth having? Are the any other
#        String methods that Symbols really should have too?

#
class Symbol

  # Easily manipulate undercores on symbols.
  #
  #   :a.pad(2)         #=> :__a__
  #   :__a__.pad(-1)    #=> :_a_
  #
  def shadow(i=1)
    return self if i == 0
    s = self.to_s
    if i > 0
      return ( ('_'*i) + self.to_s + ('_'*i) ).to_sym
    else
      i *= -1
      return s[i..-i-1].to_sym if s[0..i-1] == ('_'*i) and s[-i..-1] == ('_'*i)
      return self
    end
  end
  alias :pad :shadow

end


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

  require 'test/unit'

  class TestSymbol < Test::Unit::TestCase

    def test_shadow
      assert_equal( :__a__, :a.shadow(2) )
      assert_equal( :_a_, :__a__.shadow(-1) )
    end

  end

=end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
facets-2.0.0 lib/core/facets/symbol/shadow.rb
facets-2.0.1 lib/core/facets/symbol/shadow.rb
facets-2.0.2 lib/core/facets/symbol/shadow.rb
facets-2.1.0 lib/core/facets/symbol/shadow.rb
facets-2.1.1 lib/core/facets/symbol/shadow.rb
facets-2.1.2 lib/core/facets/symbol/shadow.rb
facets-2.0.4 lib/core/facets/symbol/shadow.rb
facets-2.0.3 lib/core/facets/symbol/shadow.rb
facets-2.0.5 lib/core/facets/symbol/shadow.rb
facets-2.1.3 lib/core/facets/symbol/shadow.rb