Diffident
Based on the differ gem by Pieter Vande Bruggen
Diffident is a flexible, pure-Ruby diff library, suitable for use in both command line scripts and web applications. The flexibility comes from the fact that diffs can be built at completely arbitrary levels of granularity (some common ones are built-in), and can be output in a variety of formats.
It also gives you raw access to the diff structure, should you need it, so you can build whatever you need that needs a diff.
Internals
Diffident uses the ruby StringScan object to walk through the input and collect Diffidentances. It records additions, subtractions, changes and can merge two diffs that might additionally contain conflicts.
Installation
Bundler
Add this to your Gemfile
gem 'diffident'
and run bundle to install
bundle install
Manual
Install it via gem
sudo gem install diffident
and require it in your project
require 'diffident'
How do I use this thing?
There are a number of ways to use Diffident, depending on your situation and needs. Lets examplify:
@original = "Epic lolcat fail!"
@current = "Epic wolfman fail!"
There are a number of built-in diff_by_* methods to choose from for standard use:
Diffident.diff_by_line(@current, @original)
Diffident.diff_by_word(@current, @original)
Diffident.diff_by_char(@current, @original)
Ok, but that doesn't quite cover my case, I have to split by "whatever".
No problem, you can call diff directly and supply your own boundary string:
Diffident.diff(@current, @original)
Diffident.diff(@current, @original, 'i')
Yeah, thats nice. But a simple string might not always cut it...
Well, you can supply a regex instead of your string if you have to:
Diffident.diff(@original, @current, /[a-z]i/)
Include a capture group if you want to keep the separator:
Diffident.diff(@original, @current, /([a-z]i)/)
Ok, ok, but I don't like having to write "Diffident" everywhere.
If you would like something a little more inline you can require 'diffident/string'
to get some added inline string magic:
@current.diff(@original)
Or a lot more inline:
@current - @original
Diffident.separator = ' '
@current - @original
Diffident.separator = /[a-z]i/
@original - @current
Diffident.separator = /([a-z]i)/
@original - @current
So we've pretty much got you covered.
What about output formatting?
Need a diffidentent output format? We've got a few of those too:
Diffident.format = :ascii
Diffident.format = :color
Diffident.format = :html
Diffident.format = MyCustomFormatModule
The formatter must respond to the call method that takes an instant of the Change class as an argument and returns a string.
But I don't want to change the system-wide default for only a single diff output!
Yeah, we either:
diff = @current - @original
diff.format_as(:color)
Or with your own formatter:
diff = @current - @original
diff.format_as(->(c){c.to_s})
Copyright
Know always right from wrong, and let others see your good works. - Pieter Vande Bruggen
The MIT License (MIT)
Copyright (c) 2013 my codeworks.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.