Diff::LCS is a port of Algorithm::Diff that uses the McIlroy-Hunt longest common subsequence (LCS) algorithm to compute intelligent differences between two sequenced enumerable containers. The implementation is based on Mario I. Wolczko’s Smalltalk version (1.2, 1993) and Ned Konz’s Perl version (Algorithm::Diff).
This release is version 1.1.1, fixing a reported #patch bug in 1.1.0. Version 1.1.0 added new features, including the ability to #patch and #unpatch changes as well as a new contextual diff callback, Diff::LCS::ContextDiffCallbacks, that should improve the context sensitivity of patching.
Using this module is quite simple. By default, Diff::LCS does not extend objects with the Diff::LCS interface, but will be called as if it were a function:
require 'diff/lcs' 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) lcs = ::.(seq1, seq2) diffs = ::.(seq1, seq2) sdiff = ::.(seq1, seq2) seq = ::.(seq1, seq2, callback_obj) bal = ::.(seq1, seq2, callback_obj) seq2 == ::.(seq1, diffs) seq1 == ::.(seq2, diffs) seq2 == ::.(seq1, sdiff) seq1 == ::.(seq2, sdiff)
Objects can be extended with Diff::LCS:
seq1.extend(::) lcs = seq1.lcs(seq2) diffs = seq1.diff(seq2) sdiff = seq1.sdiff(seq2) seq = seq1.traverse_sequences(seq2, callback_obj) bal = seq1.traverse_balanced(seq2, callback_obj) seq2 == seq1.patch!(diffs) seq1 == seq2.unpatch!(diffs) seq2 == seq1.patch!(sdiff) seq1 == seq2.unpatch!(sdiff)
By requiring 'diff/lcs/array' or 'diff/lcs/string', Array or String will be extended for use this way.
# Copyright 2004 Austin Ziegler <[email protected]> # adapted from: # Algorithm::Diff (Perl) by Ned Konz <[email protected]> # Smalltalk by Mario I. Wolczko <[email protected]> # implements McIlroy-Hunt diff algorithm # # This program is free software. It may be redistributed and/or modified # under the terms of the GPL version 2 (or later), the Perl Artistic # licence, or the Ruby licence. # # $Id: README,v 1.6 2004/09/14 18:51:26 austin Exp $
This library is called Diff::LCS because there are multiple Ruby libraries called Algorithm::Diff maintained by other authors.
By sequenced enumerable, I mean that the order of enumeration is predictable and consistent for the same set of data. While it is theoretically possible to generate a diff for unordereded hash, it will only be meaningful if the enumeration of the hashes is consistent. In general, this will mean that containers that behave like String or Array will perform best.