Sha256: 9e131ebeff5ea0640293ef8ee5b9d4faec95ee7c1b3e21ce0feca60854ea91a2

Contents?: true

Size: 1.08 KB

Versions: 57

Compression:

Stored size: 1.08 KB

Contents

class String

  # Aligns each line n spaces.
  # (This used to be #taballto.)
  #
  def tab(n)
    gsub(/^ */, ' ' * n)
  end

  # Preserves relative tabbing.
  # The first non-empty line ends up with n spaces before nonspace.
  #
  def tabto(n)
    if self =~ /^( *)\S/
      indent(n - $1.length)
    else
      self
    end
  end

  # Indent left or right by n spaces.
  # (This used to be called #tab and aliased as #indent.)
  #
  def indent(n)
    if n >= 0
      gsub(/^/, ' ' * n)
    else
      gsub(/^ {0,#{-n}}/, "")
    end
  end

end


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

  require 'test/unit'

  class TCString < Test::Unit::TestCase

    def test_tab
      a = "xyz".tab(4)
      assert_equal( '    ', a[0..3] )
      # Need to expand on this
    end

    def test_tabto
      a = "xyz".tabto(4)
      assert_equal( '    ', a[0..3] )
      # Need to expand on this
    end

    def test_indent
      a = "xyz".indent(4)
      assert_equal( '    ', a[0..3] )
      # Need to expand on this
    end

  end

=end

Version data entries

57 entries across 57 versions & 3 rubygems

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