Pure Ruby unified diff library. No external dependencies, no shelling out to diff.
Compatible with Diffy::Diff.new(a, b, diff: '-u').to_s.
Uses the Myers diff algorithm.
Add this line to your Gemfile:
gem "udiff"require "udiff"
a = "foo\nbar\nbaz\n"
b = "foo\nqux\nbaz\n"
puts Udiff::Diff.new(a, b).to_s foo
-bar
+qux
baz
By default, file headers (--- a / +++ b) and hunk headers (@@ ... @@) are not included. To include them:
puts Udiff::Diff.new(a, b, include_diff_info: true).to_s--- a
+++ b
@@ -1,3 +1,3 @@
foo
-bar
+qux
bazputs Udiff::Diff.new(a, b).to_s(:color)Produces ANSI-colored output:
- Red: removed lines
- Green: added lines
- Cyan: hunk headers (
@@...@@) - Gray: file headers (
---/+++)
# Show 1 line of context instead of the default 3
puts Udiff::Diff.new(a, b, context: 1).to_s