Skip to content

Instantly share code, notes, and snippets.

@pca2
Created January 23, 2024 20:30
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 pca2/6709d2ead2623edaecbfbd1343dfebbf to your computer and use it in GitHub Desktop.
Save pca2/6709d2ead2623edaecbfbd1343dfebbf to your computer and use it in GitHub Desktop.
Compare two Ruby hashes and return that doesn't match
def hash_diff(hash1, hash2)
diff = {}
# Check for keys and values present in hash1 but not in hash2
hash1.each do |key, value|
diff[key] = [value, hash2[key]] unless hash2[key] == value
end
# Check for keys and values present in hash2 but not in hash1
hash2.each do |key, value|
diff[key] = [hash1[key], value] unless hash1[key] == value
end
diff
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment