Reputation: 43
Do somebody know any gems to compare any txt or doc or pdf files? I need to compare and show differences like git
Upvotes: 3
Views: 1124
Reputation: 2637
There's a nice gem called diffy (it uses diff
behind the scenes)
just add it to your application:
gem "diffy"
and then you can simply use it to compare strings:
diff = Diffy::Diff.new(s1, s2)
puts diff
or files:
diff = Diffy::Diff.new(file1_path, file2_path, source: "files")
puts diff
It has nice options such as HTML output as well as providing basic CSS styles.
Please see the documentation.
Upvotes: 3
Reputation: 814
You can compare text files by doing this:-
f1 = IO.readlines("text_file1.txt").map(&:chomp)
f2 = IO.readlines("text_file1.txt").map(&:chomp)
File.open("diff.txt","w"){ |f| f.write((f1-f2).join("\n")) }
Upvotes: 2