[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Fwd: Re: Whats so different about a Hash?

Andrew Walrond

5/5/2005 1:28:00 PM

On Thursday 05 May 2005 13:57, ts wrote:
> >>>>> "A" == Andrew Walrond <andrew@walrond.org> writes:
>
> A> $ ruby --version
> A> ruby 1.8.2 (2004-11-27) [i686-linux]
>
> Can you update to the latest stable
>

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


4 Answers

ts

5/5/2005 1:32:00 PM

0

>>>>> "A" == Andrew Walrond <andrew@walrond.org> writes:

A> irb(main):033:0> {1=>2}=={1=>2} => true
A> irb(main):034:0> {1=>2}==={1=>2} => true
A> 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


Andrew Walrond

5/5/2005 1:41:00 PM

0

On Thursday 05 May 2005 14:31, ts wrote:
> >>>>> "A" == Andrew Walrond <andrew@walrond.org> writes:
>
> A> irb(main):033:0> {1=>2}=={1=>2} => true
> A> irb(main):034:0> {1=>2}==={1=>2} => true
> A> 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_ :)

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


Andrew Walrond

5/5/2005 1:47:00 PM

0

On Thursday 05 May 2005 14:31, ts wrote:
> >>>>> "A" == Andrew Walrond <andrew@walrond.org> writes:
>
> A> irb(main):033:0> {1=>2}=={1=>2} => true
> A> irb(main):034:0> {1=>2}==={1=>2} => true
> A> 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 ?

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

Andrew


ts

5/5/2005 1:52:00 PM

0

>>>>> "A" == Andrew Walrond <andrew@walrond.org> writes:

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

yes,

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

yes, #hash and #eql?


Guy Decoux