Class: Diff::LCS::DiffCallbacks

Inherits:
Object
  • Object
show all
Defined in:
lib/diff/lcs/callbacks.rb

Overview

This will produce a compound array of simple diff change objects. Each element in the #diffs array is a hunk or hunk array, where each element in each hunk array is a single Change object representing the addition or removal of a single element from one of the two tested sequences. The hunk provides the full context for the changes.

“‘ruby diffs = Diff::LCS.diff(seq1, seq2) # This example shows a simplified array format. # [ [ [ ’-‘, 0, ’a’ ] ], # 1 # [ [ ‘+’, 2, ‘d’ ] ], # 2 # [ [ ‘-’, 4, ‘h’ ], # 3 # [ ‘+’, 4, ‘f’ ] ], # [ [ ‘+’, 6, ‘k’ ] ], # 4 # [ [ ‘-’, 8, ‘n’ ], # 5 # [ ‘-’, 9, ‘p’ ], # [ ‘+’, 9, ‘r’ ], # [ ‘+’, 10, ‘s’ ], # [ ‘+’, 11, ‘t’ ] ] ] “‘

There are five hunks here. The first hunk says that the a at position 0 of the first sequence should be deleted (‘’-‘`). The second hunk says that the d at position 2 of the second sequence should be inserted (`’+‘`). The third hunk says that the h at position 4 of the first sequence should be removed and replaced with the f from position 4 of the second sequence. The other two hunks are described similarly.

### Use

This callback object must be initialised and is used by the Diff::LCS#diff method.

“‘ruby cbo = Diff::LCS::DiffCallbacks.new Diff::LCS.lcs(seq1, seq2, cbo) cbo.finish “`

Note that the call to #finish is absolutely necessary, or the last set of changes will not be visible. This callback also supports a block mode which automatically calls #finish.

“‘ruby result = Diff::LCS::DiffCallbacks.new { |cbo| Diff::LCS.lcs(seq1, seq2, cbo) } “`

### Simplified Array Format

The simplified array format used in the example above can be obtained with:

“‘ruby require ’pp’ pp diffs.map { |e| e.map { |f| f.to_a } } “‘

Direct Known Subclasses

ContextDiffCallbacks

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDiffCallbacks

:yields: self



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/diff/lcs/callbacks.rb', line 101

def initialize # :yields: self
  @hunk = []
  @diffs = []

  return unless block_given?

  begin
    yield self
  ensure
    finish
  end
end

Instance Attribute Details

#diffsObject (readonly)

Returns the difference set collected during the diff process.



99
100
101
# File 'lib/diff/lcs/callbacks.rb', line 99

def diffs
  @diffs
end

Instance Method Details

#discard_a(event) ⇒ Object



120
121
122
# File 'lib/diff/lcs/callbacks.rb', line 120

def discard_a(event)
  @hunk << Diff::LCS::Change.new("-", event.old_position, event.old_element)
end

#discard_b(event) ⇒ Object



124
125
126
# File 'lib/diff/lcs/callbacks.rb', line 124

def discard_b(event)
  @hunk << Diff::LCS::Change.new("+", event.new_position, event.new_element)
end

#finishObject

Finalizes the diff process. If an unprocessed hunk still exists, then it is appended to the diff list.



116
# File 'lib/diff/lcs/callbacks.rb', line 116

def finish = finish_hunk

#match(_event) ⇒ Object



118
# File 'lib/diff/lcs/callbacks.rb', line 118

def match(_event) = finish_hunk