Module: DNote::StringExt

Defined in:
lib/dnote/core_ext.rb

Overview

Extensions for String class. These methods are taken directly from Ruby Facets.

Instance Method Summary collapse

Instance Method Details

#indent(num) ⇒ Object

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

CREDIT: Gavin Sinclair
CREDIT: Trans


14
15
16
17
18
19
20
# File 'lib/dnote/core_ext.rb', line 14

def indent(num)
  if num >= 0
    gsub(/^/, " " * num)
  else
    gsub(/^ {0,#{-num}}/, "")
  end
end

#tabset(num) ⇒ Object



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

def tabset(num)
  i = lines.map do |line|
    line.strip.empty? ? nil : line.index(/\S/)
  end
  x = i.compact.min
  t = num - x.to_i
  t = 0 if t < 0
  indent(t)
end