Bug concerning 1.6.8 and hash equality?

I think I may have stumbled upon a bug in Ruby version 1.6.8. This issue
arose by a unit test comparing the equality of different types of hashes.
At first I thought the default behavior of 1.6.8 was correct; hashes
with equal keys and values are equal when compared with “==” and are not
equal if one of those hashes has been instantiated with a default value;
in my case an empty array.

In version 1.6.7 and 1.8.0-pre this behavior differs and both cases
evaluate as true. An example should make this all clear. :slight_smile:

emiel@chyrellos:/usr/local/ruby-1.6.8/bin$ ./irb
irb(main):001:0> a = Hash.new []
=> {}
irb(main):002:0> b = {}
=> {}
irb(main):003:0> a == b
=> false
irb(main):004:0> p RUBY_VERSION
"1.6.8"
=> nil

The question is which is the correct behavior? Personally I believe the
behavior of 1.6.7 is correct; a hash is a hash no matter which default
value it carries.

Emiel

···


E F van de Laar
+31648183479
www.il.fontys.nl/~emiel

Hi –

···

On Fri, 24 Jan 2003, E F van de Laar wrote:

I think I may have stumbled upon a bug in Ruby version 1.6.8. This issue
arose by a unit test comparing the equality of different types of hashes.
At first I thought the default behavior of 1.6.8 was correct; hashes
with equal keys and values are equal when compared with “==” and are not
equal if one of those hashes has been instantiated with a default value;
in my case an empty array.

In version 1.6.7 and 1.8.0-pre this behavior differs and both cases
evaluate as true. An example should make this all clear. :slight_smile:

emiel@chyrellos:/usr/local/ruby-1.6.8/bin$ ./irb
irb(main):001:0> a = Hash.new
=> {}
irb(main):002:0> b = {}
=> {}
irb(main):003:0> a == b
=> false
irb(main):004:0> p RUBY_VERSION
“1.6.8”
=> nil

The question is which is the correct behavior? Personally I believe the
behavior of 1.6.7 is correct; a hash is a hash no matter which default
value it carries.

It’s definitely not a bug; see ChangeLog:

Wed Aug 7 09:51:54 2002 Yukihiro Matsumoto matz@ruby-lang.org
* hash.c (rb_hash_equal): should check default values.

I think the reasoning is as follows:

The notion of hash equality is really based in the idea of all the
key/value pairs being the same. In other words, when this is true:

a == b

then this should also be true:

a[v] == b[v]

for every v.

But in 1.6.7, the following situation is possible:

a == b # true
a[v] == b[v] # false

That means that a == b being true doesn’t really tell you anything
useful, because you can’t use it to predict what the specific
key-by-key comparisons will give you.

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav