Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/rubytest/core_ext/string.rb

Instance Method Summary collapse

Instance Method Details

#indent(n, c = ' ') ⇒ Object

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).



21
22
23
24
25
26
27
# 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) ⇒ Object

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

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



8
9
10
11
12
13
14
# File 'lib/rubytest/core_ext/string.rb', line 8

def tabto(n)
  if self =~ /^( *)\S/
    indent(n - $1.length)
  else
    self
  end
end