class Diff

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.

Public Class Methods

new(a, b) click to toggle source

Create a new Diff between the a list and b list.

# File lib/taskjuggler/AlgorithmDiff.rb, line 96
def initialize(a, b)
  @hunks = []
  diff(a, b)
end

Public Instance Methods

editScript() click to toggle source
# File lib/taskjuggler/AlgorithmDiff.rb, line 115
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() click to toggle source
# File lib/taskjuggler/AlgorithmDiff.rb, line 136
def inspect
  puts to_s
end
patch(values) click to toggle source

Modify the values list according to the stored diff information.

# File lib/taskjuggler/AlgorithmDiff.rb, line 102
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() click to toggle source

Return the diff list as standard UNIX diff output.

# File lib/taskjuggler/AlgorithmDiff.rb, line 130
def to_s
  str = ''
  @hunks.each { |hunk| str << hunk.to_s }
  str
end