# Should hash equality ignore default values?

**URL:** <https://www.ruby-forum.com/t/should-hash-equality-ignore-default-values/103566>\
**Category:** Ruby\
**Created:** [May 7, 2007, 11:32pm UTC](https://www.ruby-forum.com/t/should-hash-equality-ignore-default-values/103566 "2007-05-07T23:32:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fmwr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9de053/32.png) [@fmwr](https://www.ruby-forum.com/u/fmwr)\
**Post date:** [May 7, 2007, 11:32pm UTC](https://www.ruby-forum.com/t/should-hash-equality-ignore-default-values/103566/1 "2007-05-07T23:32:13Z")

</div>

Hash equality right now ignores the default values.  
So a == b may be true even if a[0] == b[0] would be false.

Is this behaviour intended ?

irb(main):001:0\> a = Hash.new(0)  
=\> {}  
irb(main):002:0\> b = Hash.new(42)  
=\> {}  
irb(main):003:0\> a == b  
=\> true  
irb(main):004:0\> a[0] == b[0]  
=\> false  
irb(main):005:0\> c = Hash.new{|ht,k| ht[k] = 5}  
=\> {}  
irb(main):006:0\> a == c  
=\> true  
irb(main):007:0\> a[0] == c[0]  
=\> false

---

<div class="post-metadata">

**Author:** ![fmwr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9de053/32.png) [@fmwr](https://www.ruby-forum.com/u/fmwr)\
**Post date:** [May 8, 2007, 2:11am UTC](https://www.ruby-forum.com/t/should-hash-equality-ignore-default-values/103566/2 "2007-05-08T02:11:29Z")

</div>

On 5/7/07, Tomasz W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## =\> true
> 
> Tomasz W. [[http://t-a-w.blogspot.com/](http://t-a-w.blogspot.com/) ]

Good question Tomasz and furthermore…  
irb(main):006:0\> h=Hash.new{|h,k|h[k]=1}  
=\> {}  
irb(main):007:0\> g=Hash.new{|h,k|h[k]=2}  
=\> {}  
irb(main):008:0\> h==g  
=\> true  
irb(main):009:0\> h[1] ### We just must not forget that this is an  
assignment!!!  
=\> 1  
irb(main):010:0\> h==g  
=\> false

The second result of the comparision is clear h== {1 =\>1} and g=={}

although this might be surprising I think it is about ok, without this  
we could not compare to literals…  
tough call though  
I am eager to hear other opinions.

Robert
