Class: SubDiff::Differ Private

Inherits:
Object
  • Object
show all
Includes:
Buildable
Defined in:
lib/sub_diff/differ.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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.

Instance Attribute Summary

Attributes included from Buildable

#builder

Instance Method Summary collapse

Methods included from Buildable

included, #initialize

Instance Method Details

#match(search, *args, replacement) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


21
22
23
24
25
26
27
# File 'lib/sub_diff/differ.rb', line 21

def match(search, *args, replacement)
  string.send(diff_method, search) do |match|
    diff = { match: match, prefix: $`, suffix: $' }
    diff[:replacement] = match.sub(search, *args, &replacement)
    yield(diff)
  end
end