Whats so different about a Hash?

See other message - same result as you.

But is this the _correct_ behaviour?

The reason this came up was because I wanted to use hashes as keys in hashes.
Consider:

irb(main):001:0> a={} => {}
irb(main):002:0> a[{1=>2}]=3 => 3
irb(main):003:0> a[{1=>2}]=3 => 3
irb(main):004:0> a[{1=>2}]=3 => 3
irb(main):005:0> a.inspect => "{{1=>2}=>3, {1=>2}=>3, {1=>2}=>3}"

but

irb(main):018:0> a={} => {}
irb(main):019:0> a[[1,2]]=3 => 3
irb(main):020:0> a[[1,2]]=3 => 3
irb(main):021:0> a[[1,2]]=3 => 3
irb(main):022:0> a.inspect => "{[1, 2]=>3}"

Not at all what I was expecting! Is there a good reason why

irb(main):029:0> [1,2]==[1,2] => true
irb(main):030:0> [1,2]===[1,2] => true
irb(main):031:0> [1,2].eql?([1,2]) => true

but

irb(main):033:0> {1=>2}=={1=>2} => true
irb(main):034:0> {1=>2}==={1=>2} => true
irb(main):035:0> {1=>2}.eql?({1=>2}) => false

?

Andrew

···

On Thursday 05 May 2005 13:57, ts wrote:

> $ ruby --version
> ruby 1.8.2 (2004-11-27) [i686-linux]

Can you update to the latest stable

irb(main):033:0> {1=>2}=={1=>2} => true
irb(main):034:0> {1=>2}==={1=>2} => true
irb(main):035:0> {1=>2}.eql?({1=>2}) => false

svg% ./irb
irb(main):001:0> RUBY_VERSION
=> "1.9.0"
irb(main):002:0> {1=>2}=={1=>2}
=> true
irb(main):003:0> {1=>2}==={1=>2}
=> true
irb(main):004:0> {1=>2}.eql?({1=>2})
=> true
irb(main):005:0> svg%

Guy Decoux

So 1.9 does what I would consider to be the right thing :slight_smile:

So, whats the best way to modify the behaviour of Hash, to fix the bug in
versions prior to 2004-12-25, and make the behaviour consistent across 1.8
and 1.9, such that Hash#, hash#update etc etc work as per 1.9.

I’m sure this must be easy…Override eql? ?

Andrew

···

On Thursday 05 May 2005 14:31, ts wrote:

irb(main):033:0> {1=>2}=={1=>2} => true
irb(main):034:0> {1=>2}==={1=>2} => true
irb(main):035:0> {1=>2}.eql?({1=>2}) => false

svg% ./irb
irb(main):001:0> RUBY_VERSION
=> “1.9.0”
irb(main):002:0> {1=>2}=={1=>2}
=> true
irb(main):003:0> {1=>2}==={1=>2}
=> true
irb(main):004:0> {1=>2}.eql?({1=>2})
=> true
irb(main):005:0> svg%

Guy Decoux

I assume {1=>2}.hash == {1=>2}.hash on 1.9 ?

(Ie should I override hash or eql? to make the behaviour consistent across
versions)

Andrew

···

On Thursday 05 May 2005 14:31, ts wrote:

irb(main):033:0> {1=>2}=={1=>2} => true
irb(main):034:0> {1=>2}==={1=>2} => true
irb(main):035:0> {1=>2}.eql?({1=>2}) => false

svg% ./irb
irb(main):001:0> RUBY_VERSION
=> “1.9.0”
irb(main):002:0> {1=>2}=={1=>2}
=> true
irb(main):003:0> {1=>2}==={1=>2}
=> true
irb(main):004:0> {1=>2}.eql?({1=>2})
=> true
irb(main):005:0> svg%

I assume {1=>2}.hash == {1=>2}.hash on 1.9 ?

yes,

(Ie should I override hash or eql? to make the behaviour consistent across
versions)

yes, #hash and #eql?

Guy Decoux