From: Charles Oliver Nutter Date: 2009-02-16T09:54:44+09:00 Subject: Re: Iterating a changing Hash under 1.9.1 Charles Oliver Nutter wrote: > Phrogz wrote: >> I think you're wrong. >> Initial state: { 0=>0 } >> First loop: k is 0, a[0] is 1, assign h[1]=>1 >> A brand new key/value has been inserted to the hash this point. >> If #each then covered the next key/value pair: >> Second loop: k is 1, a[1] is 2, assign h[2]=>2 (and so on) > > There are a maximum of four keys possible and you never remove any. The > second time through you're reassigning keys that are already there from > the first time. Actually it looks like 1.9 is slightly different then what I described (which was what I know of ordered hash iteration in JRuby. Ruby 1.9 appears to 'each' only once, since it's already at the end of the list. In JRuby, we continue to iterate as long as you keep adding new keys: a = [ 1, 2, 3 ]; h = { 0=>0 } h.each{ |k,v| p [k,v]; p a[k]; h[a[k]] = a[k] } p h JRuby: [0, 0] 1 [1, 1] 2 [2, 2] 3 [3, 3] nil [nil, nil] Ruby 1.9.1: [0, 0] 1 {0=>0, 1=>1} Could be just a minor difference or a bug in one or the other. - Charlie