Class: Diff::LCS::SDiffCallbacks
- Inherits:
-
Object
- Object
- Diff::LCS::SDiffCallbacks
- Defined in:
- lib/diff/lcs/callbacks.rb
Overview
This will produce a simple array of diff change objects. Each element in the #diffs
array is a single ContextChange. In the set of #diffs provided by SDiffCallbacks, both
old and new objects will be presented for both changed and unchanged
objects. nil will be substituted for a discarded object.
The diffset produced by this callback, when provided to Diff::LCS#sdiff, will compute
and display the necessary components to show two sequences and their minimized
differences side by side, just like the Unix utility sdiff.
# same same
# before | after
# old < -
# - > new
seq1 = %w(a b c e h j l m n p)
seq2 = %w(b c d e f j k l m r s t)
diffs = Diff::LCS.sdiff(seq1, seq2)
# This example shows a simplified array format.
# [ [ "-", [ 0, "a"], [ 0, nil ] ],
# [ "=", [ 1, "b"], [ 0, "b" ] ],
# [ "=", [ 2, "c"], [ 1, "c" ] ],
# [ "+", [ 3, nil], [ 2, "d" ] ],
# [ "=", [ 3, "e"], [ 3, "e" ] ],
# [ "!", [ 4, "h"], [ 4, "f" ] ],
# [ "=", [ 5, "j"], [ 5, "j" ] ],
# [ "+", [ 6, nil], [ 6, "k" ] ],
# [ "=", [ 6, "l"], [ 7, "l" ] ],
# [ "=", [ 7, "m"], [ 8, "m" ] ],
# [ "!", [ 8, "n"], [ 9, "r" ] ],
# [ "!", [ 9, "p"], [ 10, "s" ] ],
# [ "+", [ 10, nil], [ 11, "t" ] ] ]
The result of this operation is similar to that of Diff::LCS::ContextDiffCallbacks. They may be compared as:
s = Diff::LCS.sdiff(seq1, seq2).reject { |e| e.action == "=" }
c = Diff::LCS.sdiff(seq1, seq2, Diff::LCS::ContextDiffCallbacks).flatten(1)
s == c # => true
Use
This callback object must be initialised and is used by the Diff::LCS#sdiff method.
cbo = Diff::LCS::SDiffCallbacks.new
Diff::LCS.lcs(seq1, seq2, cbo)
This callback also supports initialization with a block, but as there is no "finishing" to be done, this has no effect on the state of the object.
result = Diff::LCS::SDiffCallbacks.new { |cbo| Diff::LCS.lcs(seq1, seq2, cbo) }
Simplified Array Format
The simplified array format used in the example above can be obtained with:
require 'pp'
pp diffs.map { |e| e.to_a }
Instance Attribute Summary collapse
-
#diffs ⇒ Object
readonly
Returns the difference set collected during the diff process.
Instance Method Summary collapse
- #change(event) ⇒ Object
- #discard_a(event) ⇒ Object
- #discard_b(event) ⇒ Object
-
#initialize {|_self| ... } ⇒ SDiffCallbacks
constructor
:yields: self.
- #match(event) ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ SDiffCallbacks
:yields: self
305 306 307 308 |
# File 'lib/diff/lcs/callbacks.rb', line 305 def initialize # :yields: self @diffs = [] yield self if block_given? end |
Instance Attribute Details
#diffs ⇒ Object (readonly)
Returns the difference set collected during the diff process.
303 304 305 |
# File 'lib/diff/lcs/callbacks.rb', line 303 def diffs @diffs end |
Instance Method Details
#change(event) ⇒ Object
322 323 324 |
# File 'lib/diff/lcs/callbacks.rb', line 322 def change(event) @diffs << Diff::LCS::ContextChange.simplify(event) end |
#discard_a(event) ⇒ Object
314 315 316 |
# File 'lib/diff/lcs/callbacks.rb', line 314 def discard_a(event) @diffs << Diff::LCS::ContextChange.simplify(event) end |
#discard_b(event) ⇒ Object
318 319 320 |
# File 'lib/diff/lcs/callbacks.rb', line 318 def discard_b(event) @diffs << Diff::LCS::ContextChange.simplify(event) end |
#match(event) ⇒ Object
310 311 312 |
# File 'lib/diff/lcs/callbacks.rb', line 310 def match(event) @diffs << Diff::LCS::ContextChange.simplify(event) end |