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.
Diffident uses the Ruby StringScan class to walk through the input and collect differances. It records additions, subtractions, changes and can merge two diffs that might additionally contain conflicts.
Add this to your Gemfile
gem 'diffident'
and run bundle to install
bundle install
Install it via gem
sudo gem install diffident
and require it in your project
require 'diffident'
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)
# => {"Epic lolcat fail!" >> "Epic wolfman fail!"}
Diffident.diff_by_word(@current, @original)
# => Epic {"lolcat" >> "wolfman"} fail!
Diffident.diff_by_char(@current, @original)
# => Epic {+"wo"}l{-"olcat "}f{+"m"}a{+"n fa"}il!
No problem, you can call diff directly and supply your own boundary string:
Diffident.diff(@current, @original) # Implicitly by line by default
# => {"Epic lolcat fail!" >> "Epic wolfman fail!"}
Diffident.diff(@current, @original, 'i')
# => Epi{"c lolcat fa" >> "c wolfman fa"}il
Well, you can supply a regex instead of your string if you have to:
Diffident.diff(@original, @current, /[a-z]i/)
# => E{"c wolfman f" >> "c lolcat f"}l!
Include a capture group if you want to keep the delimiter:
Diffident.diff(@original, @current, /([a-z]i)/)
# => Epi{"c wolfman f" >> "c lolcat f"}ail!
If you would like something a little more inline you can require 'diffident/string'
to get some added inline string magic:
@current.diff(@original) # Implicitly by line by default
# => {"Epic lolcat fail!" >> "Epic wolfman fail!"}
Or a lot more inline:
@current - @original # Implicitly by line by default
# => {"Epic lolcat fail!" >> "Epic wolfman fail!"}
Diffident.delimiter = ' ' # Custom string
@current - @original
# => Epic {"lolcat" >> "wolfman"} fail!
Diffident.delimiter = /[a-z]i/ # Custom regex without capture group
@original - @current
# => E{"c wolfman f" >> "c lolcat f"}l!
Diffident.delimiter = /([a-z]i)/ # Custom regex with capture group
@original - @current
# => Epi{"c wolfman f" >> "c lolcat f"}ail!
So we've pretty much got you covered.
Need a diffidentent output format? We've got a few of those too:
Diffident.format = :ascii # Default
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.
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})
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.