Need diffs in your ruby app? Dirb has you covered. It provides a convenient way to generate a diff from two strings. Instead of reimplementing the LCS diff algorithm Dirb uses battle tested Unix diff to generate diffs, and focuses on providing a convenient interface, and getting out of your way.
It provides several built in format options. Pass :text, :color, or
:html to Dirb::Diff#to_s to force that format, or set
Dirb::Diff.default_format
sudo gem install dirb
Here's an example of using Dirb to diff two strings
$ 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 Dirb::Diff.new(string1, string2)
-Hello how are you
+Hello how are you?
I'm fine
-That's great
+That's swell
Outputing the diff as html is easy too.
>> puts Dirb::Diff.new(string1, string2).to_s(:html)
<div class="diff">
<ul>
<li class="del"><del>Hello how are you</del></li>
<li class="ins"><ins>Hello how are you?</ins></li>
<li class="unchanged"><span>I'm fine</span></li>
<li class="del"><del>That's great</del></li>
<li class="ins"><ins>That's swell</ins></li>
</ul>
</div>
Then try adding this css to your stylesheets:
.diff{overflow:auto;}
.diff ul{background:#fff;overflow:auto;font-size:13px;list-style:none;margin:0;padding:0;display:table;width:100%;}
.diff del, .diff ins{display:block;text-decoration:none;}
.diff li{padding:0; display:table-row;margin: 0;}
.diff del, .diff ins, .diff span{white-space:pre;font-family:courier;}
.diff li.ins{background:#9f9;}
.diff li.del{background:#fcc;}
.diff li.ins strong{font-weight:normal; background: #6f6 }
.diff li.del strong{font-weight:normal; background: #f99 }
Dirb::Diff also alows you to set a default format. Here we set the default to
use ANSI termnial color escape sequences.
>> Dirb::Diff.default_format = :color
=> :color
>> puts Dirb::Diff.new(string1, string2) # prints color in the terminal
-Hello how are you
+Hello how are you?
I'm fine
-That's great
+That's swell
Creating custom formatted output is easy too. Dirb::Diff provides an
enumberable interface which lets you iterate over lines in the diff.
>> Dirb::Diff.new("foo\nbar\n", "foo\nbar\nbaz\n").each do |line|
>* case line
>> when /^\+/ then puts "line #{line.chomp} added"
>> when /^-/ then puts "line #{line.chomp} removed"
>> end
>> end
line +baz added
=> [" foo\n", " bar\n", "+baz\n"]
Use #map, #inject, or any of Enumerable's methods. Go crazy.
Report bugs or request features at http://github.com/samg/Dirb/issues