class String

Public Instance Methods

indent(n, c=' ') click to toggle source

Indent left or right by n spaces. (This used to be called tab and aliased as indent.)

This is a Ruby Facet (rubyworks.github.com/facets).

# File lib/rubytest/core_ext/string.rb, line 21
def indent(n, c=' ')
  if n >= 0
    gsub(/^/, c * n)
  else
    gsub(/^#{Regexp.escape(c)}{0,#{-n}}/, "")
  end
end
tabto(n) click to toggle source

Preserves relative tabbing. The first non-empty line ends up with n spaces before nonspace.

This is a Ruby Facet (rubyworks.github.com/facets).

# File lib/rubytest/core_ext/string.rb, line 8
def tabto(n)
  if self =~ /^( *)\S/
    indent(n - $1.length)
  else
    self
  end
end