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


73
74
75
76
77
78
79
# File 'lib/qed/core_ext.rb', line 73

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)

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/qed/core_ext.rb', line 49

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