Skip to content

Instantly share code, notes, and snippets.

@Bartuz
Created September 19, 2014 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bartuz/350c2be34185c5f76fbf to your computer and use it in GitHub Desktop.
Save Bartuz/350c2be34185c5f76fbf to your computer and use it in GitHub Desktop.
Ruby: Compare 2 hashes
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