Method: Lucid::StepMatch#replace_arguments

Defined in:
lib/lucid/step_match.rb

#replace_arguments(string, step_arguments, format, &proc) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lucid/step_match.rb', line 44

def replace_arguments(string, step_arguments, format, &proc)
  s = string.dup
  offset = past_offset = 0
  step_arguments.each do |step_argument|
    next if step_argument.offset.nil? || step_argument.offset < past_offset

    replacement = if block_given?
      proc.call(step_argument.val)
    elsif Proc === format
      format.call(step_argument.val)
    else
      format % step_argument.val
    end

    s[step_argument.offset + offset, step_argument.val.length] = replacement
    offset += replacement.unpack('U*').length - step_argument.val.unpack('U*').length
    past_offset = step_argument.offset + step_argument.val.length
  end
  s
end