Created
September 19, 2014 11:59
-
-
Save Bartuz/350c2be34185c5f76fbf to your computer and use it in GitHub Desktop.
Ruby: Compare 2 hashes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hash | |
def diff(other) | |
(self.keys + other.keys).uniq.inject({}) do |memo, key| | |
unless self[key] == other[key] | |
if self[key].kind_of?(Hash) && other[key].kind_of?(Hash) | |
memo[key] = self[key].diff(other[key]) | |
else | |
memo[key] = [self[key], other[key]] | |
end | |
end | |
memo | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment