I've tried using that library in the past, but its interface just �
makes it crazy hard to work with.
I came up with this for diff'ing 2 files and spitting the results to
stdout. It's meant to simulate "diff -u" output, but it's not quite
identical and is definitely not tested thoroughly:
# diff_test.rb
require 'diff/lcs'
# The file 'test1.txt' contains the following 3 lines:
#
# This is a house
# Hello World
# My favorite color is blue
#
# The file 'test2.txt' contains the following 3 lines:
#
# This is a car
# Hello World
# My favorite color is green
module Diff
module LCS
def self.diff_files(file1, file2)
array1 = IO.readlines(file1)
array2 = IO.readlines(file2)
diffs = diff(array1, array2)
print diffs.first[0].action * 3 + ' '
print file1
print "\t" + File.mtime(file1).to_s
puts
print diffs.first[1].action * 3 + ' '
print file2
print "\t" + File.mtime(file2).to_s
puts
diffs.each_with_index{ |diff, i|
diff.each{ |d|
puts d.action + d.element
}
}
end
end
end
Diff::LCS.diff_files('test1.txt', 'test2.txt')
Here was the output of Diff::LCS.diff_files:
--- test1.txt Wed Feb 25 12:18:42 -0700 2009
+++ test2.txt Wed Feb 25 12:18:53 -0700 2009
-This is a house
+This is a car
-My favorite color is blue
+My favorite color is green
Hope that was useful.
Regards,
Dan