The easiest way to diff two strings in ruby

November 26, 2010

I just released version 2.0 of Diffy.

I wanted to make it dead simple to generate attractive diff output from within a ruby application.

Here’s how you use it:

$ sudo gem install diffy
$ irb
>> string1 = <<-TXT
>" Hello how are you
>" I'm fine
>" That's great
>" TXT
=> "Hello how are you\nI'm fine\nThat's great\n" 
>> string2 = <<-TXT
>" Hello how are you?
>" I'm fine
>" That's swell
>" TXT
=> "Hello how are you?\nI'm fine\nThat's swell\n" 
>> puts Diffy::Diff.new(string1, string2)
-Hello how are you
+Hello how are you?
 I'm fine
-That's great
+That's swell

It also will generate nice html for you:

>> Diffy::Diff.new(string1, string2).to_s(:html)
  • We were all having a good time.
  • Soon it was very late.
  • Time flies like an arrow” said I.
  • Fruit flies like a banana” I said.
  • Then I left.

You can find out more in the README