0.01
No release in over a year
Find the exact difference between two Hash objects
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.18.0
~> 5.15.0
~> 10.5.0
~> 1.26.1
~> 0.6.0
~> 2.3.11
~> 2.2.1
~> 1.1.0
~> 3.9.0
~> 2.10.0
 Project Readme

HashDeepDiff

Gem Version GitHub Workflow Status GitHub

Find the exact difference between two Hash objects and build a report to visualize it. Works for other objects too but why would you do that :/

Alternative solutions hashdiff by liufengyun and hash_diff by CodingZeal

Installation

Add this line to your application's Gemfile:

gem 'hash_deep_diff'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hash_deep_diff

Usage

Basic

left = { a: :a }
right = { a: :b }

print HashDeepDiff::Comparison.new(left, right).report
- left[a] = a
+ left[a] = b

Arrays

left = [1, 2, { a: :a }]
right = [2, { a: :b }, 3]

print HashDeepDiff::Comparison.new(left, right).report
-left[...] = [1]
+left[...] = [3]
-left[{}][a] = a
+left[{}][a] = b

Nesting

left  = { a: [1, 2, { a: :a } ], b: { c: [1, 2, { d: :e } ] } }
right = { a: [2, { a: :b }, 3], b: { c: { f: { g: :h } } } }

print HashDeepDiff::Comparison.new(left, right).report
-left[a][...] = [1]
+left[a][...] = [3]
-left[a][{}][a] = a
+left[a][{}][a] = b
+left[b][c][...][f] = {}
+left[b][c][...][f][g] = h
-left[b][c][...][f] = [1, 2]
-left[b][c][{}][d] = e

Reporting Engines

You can choose from the default diff-like reporting engine (examples are above) and YML reporting engine

left  = { a: [1, 2, { a: :a } ], b: { c: [1, 2, { d: :e } ] } }
right = { a: [2, { a: :b }, 3], b: { c: { f: { g: :h } } } }

Raw Report

print HashDeepDiff::Comparison.new(left, right, reporting_engine: HashDeepDiff::Reports::Yml).raw_report
=> {"additions"=>{:a=>[3, {:a=>:b}], :b=>{:c=>[{:f=>{:g=>:h}}]}},
    "deletions"=>{:a=>[1, {:a=>:a}], :b=>{:c=>[1, 2, {:d=>:e}]}}}

YML Report

print HashDeepDiff::Comparison.new(left, right, reporting_engine: HashDeepDiff::Reports::Yml).report

---
additions:
  :a:
  - 3
  - :a: :b
  :b:
    :c:
    - :f:
        :g: :h
deletions:
  :a:
  - 1
  - :a: :a
  :b:
    :c:
    - 1
    - 2
    - :d: :e

please see Documentation for more information or Reporting test

Customization

You can implement and use your own reporting engines with the default HashDeepDiff::Delta objects as a source of the report. In order to do so implement your own version of the reporting engine (example can be found here) and inject it into a Comparison

left = { a: :a }
right = { a: :b }

HashDeepDiff::Comparison.new(left, right, reporting_engine: CustomEngine).report

Contributing

Bug reports and pull requests are welcome on GitHub at bpohoriletz.