P
Phrogz
The following code shows that Hash#each under 1.9.1p0 does not iterate
over keys added during iteration:
a = [ 1, 2, 3 ]; h = { 0=>0 }
h.each{ |k,v| h[a[k]] = a[k] }
p h
#=> {0=>0, 1=>1}
However, this code (on 1.9.1p0) results in a ruby process with
unending 100% CPU usage, presumably due to an unending loop that keeps
traversing newly-added items:
h = { 1=>nil, 2=>nil }
h.each{ |k,v| h.delete(k); h[k]=v }
(Credit to "tama" for posting this on the ramaze group.)
I'm assuming that one of these two are a bug, since they seem
contradictory. I'd like to think that the latter behavior is the bug,
and that hashes aren't supposed to iterate over keys added or moved
during iteration. Can anyone confirm or deny this?
over keys added during iteration:
a = [ 1, 2, 3 ]; h = { 0=>0 }
h.each{ |k,v| h[a[k]] = a[k] }
p h
#=> {0=>0, 1=>1}
However, this code (on 1.9.1p0) results in a ruby process with
unending 100% CPU usage, presumably due to an unending loop that keeps
traversing newly-added items:
h = { 1=>nil, 2=>nil }
h.each{ |k,v| h.delete(k); h[k]=v }
(Credit to "tama" for posting this on the ramaze group.)
I'm assuming that one of these two are a bug, since they seem
contradictory. I'd like to think that the latter behavior is the bug,
and that hashes aren't supposed to iterate over keys added or moved
during iteration. Can anyone confirm or deny this?