Joel VanderWerf
1/8/2005 8:30:00 AM
Matt Mower wrote:
> Hi Traz,
>
> On Sat, 8 Jan 2005 15:56:26 +0900, Traz <A.Reith@gmail.com> wrote:
>
>>Hello all,
>>I want to use some hash who have a hash key. But, i don't understand
>>the hash key behaviour :
>>
>>
>>>>a={}
>>
>>=> {}
>>
>>>>a[{1,2}]='ok'
>>
>>=> "ok"
>>
>>>>a[{1,2}]
>>
>>=> nil
>
>
> I believe the answer is because each time you use {1,2} you are
> creating a new Hash object with the same contents but a different
> object identity:
>
> irb(main):017:0> {1,2}.object_id
> => 22400644
> irb(main):018:0> {1,2}.object_id
> => 22396300
>
> Because Hash uses Object#eql? to test for equality it makes them
> different keys and hence your example fails.
>
> Hope this helps,
>
> Matt
>
This behavior is changing in 1.9:
$ ruby -v -e 'a={ {1,2}=>3 }; p a[{1,2}]'
ruby 1.8.2 (2004-12-25) [i686-linux]
nil
$ ruby-snapshot -v -e 'a={ {1,2}=>3 }; p a[{1,2}]'
ruby 1.9.0 (2004-12-26) [i686-linux]
3