Created
January 23, 2024 20:30
-
-
Save pca2/6709d2ead2623edaecbfbd1343dfebbf to your computer and use it in GitHub Desktop.
Compare two Ruby hashes and return that doesn't match
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
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