Ivan Grishkov
Ivan Grishkov

Reputation: 43

How to compare any files in ruby app

Do somebody know any gems to compare any txt or doc or pdf files? I need to compare and show differences like git

git comparison example

Upvotes: 3

Views: 1124

Answers (2)

Amr Noman
Amr Noman

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

user3506853
user3506853

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

Related Questions