Skip to content
main
Switch branches/tags
Code

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
bin
 
 
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published