class SubDiff::Differ

Performs a {String#sub} or {String#gsub} replacement while yielding each match “diff payload” to a block.

The payload contains:

match       - the string matching the search.
prefix      - the string preceding the match.
suffix      - the string trailing the match.
replacement - the string replacing the match.

This class uses some special global variables: $` and $'. See ruby-doc.org/core-2.2.0/doc/globals_rdoc.html

Used internally by {Sub}.

@api private

Public Instance Methods

match(search, *args, replacement) { |diff| ... } click to toggle source
   # File lib/sub_diff/differ.rb
21 def match(search, *args, replacement)
22   string.send(diff_method, search) do |match|
23     diff = { match: match, prefix: $`, suffix: $' }
24     diff[:replacement] = match.sub(search, *args, &replacement)
25     yield(diff)
26   end
27 end