Class: Diff
Overview
This class is an implementation of the classic UNIX diff functionality. It’s based on an original implementation by Lars Christensen, which based his version on the Perl Algorithm::Diff implementation. This is largely a from-scratch implementation that tries to have a less intrusive and more user-friendly interface. But some code fragments are very similar to the original and are copyright © 2001 Lars Christensen.
Defined Under Namespace
Classes: Hunk
Instance Method Summary collapse
- #editScript ⇒ Object
-
#initialize(a, b) ⇒ Diff
constructor
Create a new Diff between the a list and b list.
- #inspect ⇒ Object
-
#patch(values) ⇒ Object
Modify the values list according to the stored diff information.
-
#to_s ⇒ Object
Return the diff list as standard UNIX diff output.
Constructor Details
#initialize(a, b) ⇒ Diff
Create a new Diff between the a list and b list.
97 98 99 100 |
# File 'lib/taskjuggler/AlgorithmDiff.rb', line 97 def initialize(a, b) @hunks = [] diff(a, b) end |
Instance Method Details
#editScript ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/taskjuggler/AlgorithmDiff.rb', line 116 def editScript script = [] @hunks.each do |hunk| if hunk.delete? script << "#{hunk.aIdx + 1}d#{hunk.deleteValues.length}" end if hunk.insert? script << "#{hunk.bIdx + 1}i#{hunk.insertValues.join(',')}" end end script end |
#inspect ⇒ Object
137 138 139 |
# File 'lib/taskjuggler/AlgorithmDiff.rb', line 137 def inspect puts to_s end |
#patch(values) ⇒ Object
Modify the values list according to the stored diff information.
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/taskjuggler/AlgorithmDiff.rb', line 103 def patch(values) res = values.dup @hunks.each do |hunk| if hunk.delete? res.slice!(hunk.bIdx, hunk.deleteValues.length) end if hunk.insert? res.insert(hunk.bIdx, *hunk.insertValues) end end res end |
#to_s ⇒ Object
Return the diff list as standard UNIX diff output.
131 132 133 134 135 |
# File 'lib/taskjuggler/AlgorithmDiff.rb', line 131 def to_s str = +'' @hunks.each { |hunk| str << hunk.to_s } str end |