Class: String

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

Instance Method Summary collapse

Instance Method Details

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

from facets



41
42
43
44
45
46
47
# File 'lib/qed/core_ext.rb', line 41

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

#tabto(num = nil, opts = {}) ⇒ Object

from facets

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/qed/core_ext.rb', line 17

def tabto(num=nil, opts={})
  raise ArgumentError, "String#margin has been renamed to #trim." unless num

  tab = opts[:tab] || 2
  str = gsub("\t", " " * tab)  # TODO: only leading tabs ?

  if opts[:lead]
    if self =~ /^( *)\S/
      indent(num - $1.length)
    else
      self
    end
  else
    min = []
    str.each_line do |line|
      next if line.strip.empty?
      min << line.index(/\S/)
    end
    min = min.min
    str.indent(num - min)
  end
end