Parent

Diff::LCS::DiffCallbacks

This will produce a compound array of simple diff change objects. Each element in the # 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.

    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.

    cbo = Diff::LCS::DiffCallbacks.new
    Diff::LCS.LCS(seq1, seq2, cbo)
    cbo.finish

Note that the call to # is absolutely necessary, or the last set of changes will not be visible. Alternatively, can be used as:

    cbo = Diff::LCS::DiffCallbacks.new { |tcbo| Diff::LCS.LCS(seq1, seq2, tcbo) }

The necessary # call will be made.

Simplified Array Format

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

    require 'pp'
    pp diffs.map { |e| e.map { |f| f.to_a } }

Attributes

diffs[R]

Returns the difference set collected during the diff process.

Public Class Methods

new() click to toggle source
     # File lib/diff/lcs/callbacks.rb, line 111
111:   def initialize # :yields self:
112:     @hunk = []
113:     @diffs = []
114: 
115:     if block_given?
116:       begin
117:         yield self
118:       ensure
119:         self.finish
120:       end
121:     end
122:   end

Public Instance Methods

discard_a(event) click to toggle source
     # File lib/diff/lcs/callbacks.rb, line 134
134:   def discard_a(event)
135:     @hunk << Diff::LCS::Change.new('-', event.old_position, event.old_element)
136:   end
discard_b(event) click to toggle source
     # File lib/diff/lcs/callbacks.rb, line 138
138:   def discard_b(event)
139:     @hunk << Diff::LCS::Change.new('+', event.new_position, event.new_element)
140:   end
finish() click to toggle source

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

     # File lib/diff/lcs/callbacks.rb, line 126
126:   def finish
127:     add_nonempty_hunk
128:   end
match(event) click to toggle source
     # File lib/diff/lcs/callbacks.rb, line 130
130:   def match(event)
131:     add_nonempty_hunk
132:   end

Private Instance Methods

add_nonempty_hunk() click to toggle source
     # File lib/diff/lcs/callbacks.rb, line 143
143:   def add_nonempty_hunk
144:     @diffs << @hunk unless @hunk.empty?
145:     @hunk = []
146:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.