Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save denisdefreyne/21582e8c6f9d031a76e4 to your computer and use it in GitHub Desktop.
Save denisdefreyne/21582e8c6f9d031a76e4 to your computer and use it in GitHub Desktop.
Comparing hashes with default values

Imagine two hashes that are clearly different:

hash_a[:donkey]
# => 5
hash_b[:donkey]
# => 10

Yet:

hash_a == hash_b
# => true

How is this possible, when both hash_a and hash_b are instances of Hash, without monkeypatching anywhere?

Answer: default values!

hash_a = Hash.new(5)
hash_b = Hash.new(10)

Strangely, the default value is ignored when comparing hashes.

@aktau
Copy link

aktau commented Jul 1, 2014

Come to the Go side of the force, Denis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment