Skip to content

Instantly share code, notes, and snippets.

@alloy
Created February 23, 2009 15:19
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 alloy/68988 to your computer and use it in GitHub Desktop.
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.
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