1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
= Diff
(({diff.rb})) - computes the differences between two arrays or
strings. Copyright (C) 2001-2002 Lars Christensen.
== Synopsis
diff = Diff.diff(a, b)
a.diff(b)
b = a.patch(diff)
== Module Diff
=== Module Methods
--- Diff.diff(a, b, &block)
Creates a different set which represents the differences between
((|a|)) and ((|b|)). ((|a|)) and ((|b|)) can be either be arrays
with elements of any type, strings, or object of any class that
include module ((|Diffable|))
If a block is not given, the default is to compact the
difference set elements into an array of element of the type
(({[action,position,elements]})). If action is :+, the array
represent elements which is in b but not a which are inserted at
((|position|)). If action is :-, the array represents elements
which are in a at ((|position|)) but has been removed from b. If
the original data was arrays, ((|elements|)) will be an array of
elements. If the original data was Strings, then ((|elements|))
will be a string.c
If a block is given, it will be passed each of the element in
the difference set. Each time it is called, three arguments are
passed: action, position and element. Action is either :+ or :-
for add or remove element, respectively. If the action is :+,
position will denote the position to add element in the
destination set, given all previous before this are changed. If
the action is :-, position will denote the position to delete
elements from the original set.
== Module Diffable
The ((|Diffable|)) module can be included into classes that you want
to compute difference sets for Diffable is included into String and
Array when (({diff.rb})) is (({require}))'d.
Classes including Diffable should implement (({[]})) to get element at
integer indices, (({<<})) and (({push})) to append elements to the
object and (({ClassName#new})) should accept 0 arguments to create a
new empty object. Finally, the class must implement the length method
which should return the number of element in the array.
=== Instance Methods
--- Diffable#diff(b, &block)
Convinience method which calls Diff.diff(self, b, &block).
--- Diffable#patch(diff)
Applies the differences from ((|diff|)) to the object ((|obj|))
and return the result. ((|obj|)) is not changed. ((|obj|)) and
can be either an array or a string, but must match the object
from which the ((|diff|)) was created.
|