Class: String

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

Instance Method Summary collapse

Instance Method Details

#gzub(regexp, format = nil, &proc) ⇒ Object

TODO: Use subs instead…



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

def gzub(regexp, format=nil, &proc)
  md = match(regexp)
  raise "#{self.inspect} doesn't match #{regexp.inspect}" if md.nil?
  
  s = dup
  pos = 0
  md.captures.each_with_index do |m, n|
    replacement = if block_given?
      proc.call(m)
    elsif Proc === format
      format.call(m)
    else
      format % m
    end
    
    if md.offset(n+1)[0]
      s[md.offset(n+1)[0] + pos, m.length] = replacement
      pos += replacement.length - m.length
    end
  end
  s
end

#indent(n) ⇒ Object



2
3
4
5
6
7
8
# File 'lib/cucumber/core_ext/string.rb', line 2

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

#subs(re, *args) ⇒ Object

re.source.gsub(/(*)/, ‘$var’) Cumulative #sub



12
13
14
15
16
# File 'lib/cucumber/core_ext/string.rb', line 12

def subs(re, *args)
  args.inject(self) do |s,arg|
    s.sub(re, arg)
  end
end