module Diff::LCS

Constants

BalancedCallbacks

This callback object implements the default set of callback events, which only returns the event itself. Note that finished_a and finished_b are not implemented – I haven't yet figured out where they would be useful.

Note that this is intended to be called as is, e.g.,

Diff::LCS.LCS(seq1, seq2, Diff::LCS::DefaultCallbacks)
SequenceCallbacks

This callback object implements the default set of callback events, which only returns the event itself. Note that finished_a and finished_b are not implemented – I haven't yet figured out where they would be useful.

Note that this is intended to be called as is, e.g.,

Diff::LCS.LCS(seq1, seq2, Diff::LCS::DefaultCallbacks)
VERSION

Public Class Methods

callbacks_for(callbacks) click to toggle source
# File lib/diff/lcs/callbacks.rb, line 48
def self.callbacks_for(callbacks)
  callbacks.new rescue callbacks
end

Public Instance Methods

diff(other, callbacks = nil, &block) click to toggle source

Returns the difference set between self and other. See #diff.

# File lib/diff/lcs.rb, line 148
def diff(other, callbacks = nil, &block)
  Diff::LCS.diff(self, other, callbacks, &block)
end
lcs(other, &block) click to toggle source

Returns an Array containing the longest common subsequence(s) between self and other. See Diff::LCS#LCS.

lcs = seq1.lcs(seq2)
# File lib/diff/lcs.rb, line 142
def lcs(other, &block) #:yields self[i] if there are matched subsequences:
  Diff::LCS.lcs(self, other, &block)
end
patch(patchset) click to toggle source

Attempts to patch self with the provided patchset. A new sequence based on self and the patchset will be created. See #patch. Attempts to autodiscover the direction of the patch.

# File lib/diff/lcs.rb, line 176
def patch(patchset)
  Diff::LCS.patch(self, patchset)
end
Also aliased as: unpatch
patch!(patchset) click to toggle source

Attempts to patch self with the provided patchset. A new sequence based on self and the patchset will be created. See #patch. Does no patch direction autodiscovery.

# File lib/diff/lcs.rb, line 184
def patch!(patchset)
  Diff::LCS.patch!(self, patchset)
end
patch_me(patchset) click to toggle source

Attempts to patch self with the provided patchset, using patch!. If the sequence this is used on supports replace, the value of self will be replaced. See #patch. Does no patch direction autodiscovery.

# File lib/diff/lcs.rb, line 198
def patch_me(patchset)
  if respond_to? :replace
    replace(patch!(patchset))
  else
    patch!(patchset)
  end
end
sdiff(other, callbacks = nil, &block) click to toggle source

Returns the balanced (“side-by-side”) difference set between self and other. See #sdiff.

# File lib/diff/lcs.rb, line 154
def sdiff(other, callbacks = nil, &block)
  Diff::LCS.sdiff(self, other, callbacks, &block)
end
traverse_balanced(other, callbacks = nil, &block) click to toggle source

Traverses the discovered longest common subsequences between self and other using the alternate, balanced algorithm. See #traverse_balanced.

# File lib/diff/lcs.rb, line 168
def traverse_balanced(other, callbacks = nil, &block)
  traverse_balanced(self, other, callbacks ||
                    Diff::LCS.YieldingCallbacks, &block)
end
traverse_sequences(other, callbacks = nil, &block) click to toggle source

Traverses the discovered longest common subsequences between self and other. See #traverse_sequences.

# File lib/diff/lcs.rb, line 160
def traverse_sequences(other, callbacks = nil, &block)
  traverse_sequences(self, other, callbacks ||
                     Diff::LCS.YieldingCallbacks, &block)
end
unpatch(patchset)
Alias for: patch
unpatch!(patchset) click to toggle source

Attempts to unpatch self with the provided patchset. A new sequence based on self and the patchset will be created. See #unpatch. Does no patch direction autodiscovery.

# File lib/diff/lcs.rb, line 191
def unpatch!(patchset)
  Diff::LCS.unpatch!(self, patchset)
end
unpatch_me(patchset) click to toggle source

Attempts to unpatch self with the provided patchset, using unpatch!. If the sequence this is used on supports replace, the value of self will be replaced. See #unpatch. Does no patch direction autodiscovery.

# File lib/diff/lcs.rb, line 210
def unpatch_me(patchset)
  if respond_to? :replace
    replace(unpatch!(patchset))
  else
    unpatch!(patchset)
  end
end

Private Instance Methods

diff_traversal(method, seq1, seq2, callbacks, &block) click to toggle source
# File lib/diff/lcs/internals.rb, line 3
def diff_traversal(method, seq1, seq2, callbacks, &block)
  callbacks = callbacks_for(callbacks)
  case method
  when :diff
    traverse_sequences(seq1, seq2, callbacks)
  when :sdiff
    traverse_balanced(seq1, seq2, callbacks)
  end
  callbacks.finish if callbacks.respond_to? :finish

  if block
    callbacks.diffs.map do |hunk|
      if hunk.kind_of? Array
        hunk.map { |hunk_block| block[hunk_block] }
      else
        block[hunk]
      end
    end
  else
    callbacks.diffs
  end
end