Class: String

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

Instance Method Summary collapse

Instance Method Details

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



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cucumber/core_ext/string.rb', line 2

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