Class: String

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

Overview

Borrowed methods from Ruby Facets.

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



22
23
24
25
26
27
28
# File 'lib/turn/core_ext.rb', line 22

def indent(n, c=' ')
  if n >= 0
    gsub(/^/, c * n)
  else
    gsub(/^#{Regexp.escape(c)}{0,#{-n}}/, "")
  end
end

#tab(n) ⇒ Object

Aligns each line n spaces.



6
7
8
# File 'lib/turn/core_ext.rb', line 6

def tab(n)
  gsub(/^ */, ' ' * n)
end

#tabto(n) ⇒ Object

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



12
13
14
15
16
17
18
# File 'lib/turn/core_ext.rb', line 12

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