# Duplicate keys in a Hash

**URL:** <https://www.ruby-forum.com/t/duplicate-keys-in-a-hash/55674>\
**Category:** Ruby\
**Created:** [March 22, 2006, 9:56pm UTC](https://www.ruby-forum.com/t/duplicate-keys-in-a-hash/55674 "2006-03-22T21:56:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@brian](https://www.ruby-forum.com/u/brian)\
**Post date:** [March 22, 2006, 9:56pm UTC](https://www.ruby-forum.com/t/duplicate-keys-in-a-hash/55674/1 "2006-03-22T21:56:56Z")

</div>

In the code below it appears Ruby is permitting duplicate keys. What’s  
happening?  
–Brian

h = {}  
h[{}] = “test1” #key is an empty hash  
h[{}] = “test2” #2nd key is an empty hash – two duplicate keys?  
h.size # 2

{} == {} # =\> true empty hashes are equal

---

<div class="post-metadata">

**Author:** ![brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@brian](https://www.ruby-forum.com/u/brian)\
**Post date:** [March 22, 2006, 11:26pm UTC](https://www.ruby-forum.com/t/duplicate-keys-in-a-hash/55674/2 "2006-03-22T23:26:17Z")

</div>

On Thu, 2006-03-23 at 05:56 +0900, Brian B. wrote:

> In the code below it appears Ruby is permitting duplicate keys. What’s  
> happening?  
> –Brian
> 
> h = {}  
> h[{}] = “test1” #key is an empty hash  
> h[{}] = “test2” #2nd key is an empty hash – two duplicate keys?  
> h.size # 2
> 
> {} == {} # =\> true empty hashes are equal

{}.eql? {}

# =\> false

{}.hash == {}.hash

# =\> false

I don’t think Hash uses == at all.

---

<div class="post-metadata">

**Author:** ![brian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/97f17d/32.png) [@brian](https://www.ruby-forum.com/u/brian)\
**Post date:** [March 22, 2006, 11:37pm UTC](https://www.ruby-forum.com/t/duplicate-keys-in-a-hash/55674/3 "2006-03-22T23:37:20Z")

</div>

On Mar 22, 2006, at 3:56 PM, Brian B. wrote:

> {} == {} # =\> true empty hashes are equal  
> The two empty hashes are equal, but they’re not the same empty hash,  
> if that makes sense. Try this:

irb(main):013:0\> {}.object\_id == {}.object\_id  
=\> false

I hope this answers your question!

Cheers,  
Eric
