Say I have the following hashes:
hash_x = {
:a => 1,
:b => 2
}
hash_y = {
:b => 2,
:c => 3
}
I need a chunk of logic that compares the two for equality only taking into consideration intersecting keys.
In this example the 'b' key is the only commonality between the two hashes and it's value is set to '2' in both so by that logic these two hashes would be considered equal.
Likewise these two hashes would not be equal due to the inequality of the 'd' key (the 'a' and 'c' key values are ignored since they are unique to their respective hashes):
hash_p = {
:a => 1,
:b => 2,
:d => 3,
}
hash_q = {
:b => 2,
:c => 3,
:d => 4
}
Is there a clever one-liner in Ruby that can calculate the intersecting keys of the two hashes then compare their values for equality based on those keys?
Bonus points if you provide tests.
More bonus points if you monkey-patch it into the Hash class.