Sha256: 69cbe860a5fc21d4a28dc1b4aef6f0fd2b2a550a4917ec919f1898b2ca116a3d

Contents?: true

Size: 1.88 KB

Versions: 26

Compression:

Stored size: 1.88 KB

Contents

class String

  # Align a string to the right.
  # The defualt alignment seperation is a new line ("/n")
  # This can be changes as can be the padding string which
  # defaults to a single space (' ').
  def align_right( n, sep="\n", c=' ' )
    return rjust(n.to_i,c.to_s) if sep==nil
    q = split(sep.to_s).collect { |line|
      line.rjust(n.to_i,c.to_s)
    }
    q.join(sep.to_s)
  end

  # Align a string to the left.
  # The defualt alignment seperation is a new line ("/n")
  # This can be changes as can be the padding string which
  # defaults to a single space (' ').
  def align_left( n, sep="\n", c=' ' )
    return ljust(n.to_i,c.to_s) if sep==nil
    q = split(sep.to_s).collect { |line|
      line.ljust(n.to_i,c.to_s)
    }
    q.join(sep.to_s)
  end

  # Centers each line of a string.
  #
  #   s = <<-EOS
  #   This is a test
  #   and
  #   so on
  #   EOS
  #   puts s.align_center(14)
  #
  # _produces_
  #
  #   This is a test
  #        and
  #       so on
  #
  # Align a string to the center.
  # The defualt alignment seperation is a new line ("/n")
  # This can be changed as can be the padding string which
  # defaults to a single space (' ').
  def align_center( n, sep="\n", c=' ' )
    return center(n.to_i,c.to_s) if sep==nil
    q = split(sep.to_s).collect { |line|
      line.center(n.to_i,c.to_s)
    }
    q.join(sep.to_s)
  end

  # Deprecated alias for #align_center.
  #alias_method :center_lines, :align_center

end


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

  require 'test/unit'

  class TCString < Test::Unit::TestCase

    def test_align_right
      assert_equal( "      xxx", "xxx".align_right(9) )
    end

    def test_align_left
      assert_equal( "xxx      ", "xxx".align_left(9) )
    end

    def test_align_center
      assert_equal( "   xxx   ", "xxx".align_center(9) )
    end

  end

=end

Version data entries

26 entries across 26 versions & 1 rubygems

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