Project

udiff

0.0
No release in over 3 years
Generate unified diffs between two strings in pure Ruby, compatible with Diffy::Diff.new(a, b, diff: '-u').to_s
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

udiff

CI Gem Version AI Generated

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.

Installation

Add this line to your Gemfile:

gem "udiff"

Usage

Basic

require "udiff"

a = "foo\nbar\nbaz\n"
b = "foo\nqux\nbaz\n"

puts Udiff::Diff.new(a, b).to_s
 foo
-bar
+qux
 baz

With diff info headers

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
 baz

Color output

puts Udiff::Diff.new(a, b).to_s(:color)

Produces ANSI-colored output:

  • Red: removed lines
  • Green: added lines
  • Cyan: hunk headers (@@...@@)
  • Gray: file headers (---/+++)

Custom context lines

# Show 1 line of context instead of the default 3
puts Udiff::Diff.new(a, b, context: 1).to_s