Bug #937

Recursive Hashes As Keys

Added by Charles Comstock about 3 years ago. Updated 10 months ago.

[ruby-core:20927]
Status:Rejected Start date:12/28/2008
Priority:Low Due date:
Assignee:Yusuke Endoh % Done:

0%

Category:-
Target version:Ruby 1.8.7
ruby -v:

Description

On 1.8.7 hashes which use themselves as keys appear to lack a meaningful value, yet are stored correctly in the keys and values list.

$ ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
$ ruby -e 'h = {}; h[h] = h; p h,h[h],h.keys[0],h.values[0]'
{{...}=>{...}}
nil
{{...}=>{...}}
{{...}=>{...}}

Is this intended behavior or a bug?

History

Updated by Charles Comstock about 3 years ago

Forgot to mention that h[h] on 1.8.6 is still h instead of degrading to a nil value.

Updated by Yusuke Endoh about 3 years ago

Hi,

2008/12/28 Charles Comstock <redmine@ruby-lang.org>:
> Bug #937: Recursive Hashes As Keys
> http://redmine.ruby-lang.org/issues/show/937
>
> Author: Charles Comstock
> Status: Open, Priority: Low
> Target version: Ruby 1.8.7
>
> On 1.8.7 hashes which use themselves as keys appear to lack a meaningful value, yet are stored correctly in the keys and values list.
>
> $ ruby -v
> ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
> $ ruby -e 'h = {}; h[h] = h; p h,h[h],h.keys[0],h.values[0]'
> {{...}=>{...}}
> nil
> {{...}=>{...}}
> {{...}=>{...}}
>
> Is this intended behavior or a bug?


It's an intended change from 1.8.7.  The hash value of h changes
when it is assigned:

$ ruby18 -e 'h = {}; p h.hash; h[h] = h; p h.hash'
0
1

Mutable objects (such as Array and Hash) are basically improper for
a hash key if you may modify them.  If it is absolutely necessary,
you can use Hash#rehash:

$ ruby18 -e 'h = {}; h[h] = h; h.rehash; p h[h]'
{{...}=>{...}}

-- 
Yusuke ENDOH <mame@tsg.ne.jp>

Updated by Shyouhei Urabe about 3 years ago

  • Status changed from Open to Rejected
  • Assignee set to Yusuke Endoh

Also available in: Atom PDF