Sha256: cb0c2c6612bdd2fe1c22c0c0adf31344840d1fe0c80b67d17f903fd6df3a3e07

Contents?: true

Size: 661 Bytes

Versions: 1

Compression:

Stored size: 661 Bytes

Contents

class String

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
facets-0.6.3 lib/facet/string/tab.rb