1
1
# HashDiff
2
+
2
3
[ ![ Build Status] ( https://travis-ci.org/CodingZeal/hash_diff.png?branch=master )] ( https://travis-ci.org/CodingZeal/hash_diff ) [ ![ Code Climate] ( https://codeclimate.com/github/CodingZeal/hash_diff.png )] ( https://codeclimate.com/github/CodingZeal/hash_diff ) [ ![ Gem Version] ( https://badge.fury.io/rb/hash_diff.png )] ( http://badge.fury.io/rb/hash_diff )
3
4
4
5
Deep comparison of Ruby Hash objects
@@ -31,7 +32,8 @@ Easily find the differences between two Ruby hashes.
31
32
one: ' foo1'
32
33
}
33
34
},
34
- num: 1
35
+ num: 1 ,
36
+ favorite_restaurant: " Shoney's"
35
37
}
36
38
37
39
right = {
@@ -43,7 +45,8 @@ Easily find the differences between two Ruby hashes.
43
45
two: ' foo2'
44
46
}
45
47
},
46
- word: ' monkey'
48
+ word: ' monkey' ,
49
+ favorite_restaurant: nil
47
50
}
48
51
49
52
hash_diff = HashDiff ::Comparison .new ( left, right )
@@ -52,19 +55,23 @@ Easily find the differences between two Ruby hashes.
52
55
Comparison#diff returns the left and right side differences
53
56
54
57
``` ruby
55
- hash_diff.diff # => { foo: ["bar", "bar2"], bar: ["foo", "foo2"], nested: { foo: ["bar", "bar2"], bar: { one: ["foo1", nil ], two: [nil , "foo2"] } }, num: [1, nil ], word: [nil , "monkey"] }
58
+ hash_diff.diff # => { foo: ["bar", "bar2"], bar: ["foo", "foo2"], nested: { foo: ["bar", "bar2"], bar: { one: ["foo1", HashDiff::NO_VALUE ], two: [HashDiff::NO_VALUE , "foo2"] } }, num: [1, HashDiff::NO_VALUE ], word: [HashDiff::NO_VALUE , "monkey"], favorite_restaurant: ["Shoney's", nil ] }
56
59
```
57
60
61
+ #### Missing keys
62
+
63
+ When there is a key that exists on one side it will return ` HashDiff::NO_VALUE ` to represent a missing key.
64
+
58
65
Comparison#left_diff returns only the left side differences
59
66
60
67
``` ruby
61
- hash_diff.left_diff # => { foo: "bar2", bar: "foo2", nested: { foo: "bar2", bar: { one: nil, two: "foo2" } }, num: nil, word: "monkey" }
68
+ hash_diff.left_diff # => {:foo=> "bar2", :bar=> "foo2", :nested=>{:foo=> "bar2", :bar=>{: one=>HashDiff::NO_VALUE, :two=> "foo2"} }, : num=>HashDiff::NO_VALUE, :favorite_restaurant=> nil, :word=> "monkey"}
62
69
```
63
70
64
71
Comparison#right_diff returns only the right side differences
65
72
66
73
``` ruby
67
- hash_diff.right_diff # => { foo: "bar", bar: "foo", nested: { foo: "bar", bar: { one: "foo1", two: nil } }, num: 1, word: nil }
74
+ hash_diff.right_diff # => {:foo=> "bar", :bar=> "foo", :nested=>{:foo=> "bar", :bar=>{:one=> "foo1", : two=>HashDiff::NO_VALUE} }, :num=> 1, :favorite_restaurant=>"Shoney's", :word=>HashDiff::NO_VALUE }
68
75
```
69
76
70
77
You can also use these shorthand methods
@@ -91,7 +98,7 @@ Hash#diff is not provided by default, and monkey patching is frowned upon by som
91
98
92
99
Authored by the Engineering Team of [ Coding ZEAL] ( https://codingzeal.com?utm_source=github )
93
100
94
- Copyright (c) 2017 Zeal, LLC. Licensed under the [ MIT license] ( https://opensource.org/licenses/MIT ) .
101
+ Copyright (c) 2017 Zeal, LLC. Licensed under the [ MIT license] ( https://opensource.org/licenses/MIT ) .
95
102
96
103
## Contributing
97
104
0 commit comments