Created
February 23, 2009 15:19
-
-
Save alloy/68988 to your computer and use it in GitHub Desktop.
A Hash differ that needs to be cleaned and wrapped as a real gem imo.
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) | |
result = {} | |
union = keys | other.keys | |
result[:not_in_other] = (union - keys).inject({}) { |memo, k| memo[k] = other[k]; memo } | |
result[:not_in_self] = (union - other.keys).inject({}) { |memo, k| memo[k] = self[k]; memo } | |
result[:not_equal] = {} | |
each do |key, value| | |
result[:not_equal][key] = { :self => value, :other => other[key] } unless value == other[key] | |
end | |
result | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment