Project

General

Profile

Actions

Bug #9729

closed

Hash#each が無限ループする

Bug #9729: Hash#each が無限ループする

Added by takkanm (三村 益隆) over 11 years ago. Updated over 11 years ago.

Status:
Closed
Target version:
ruby -v:
ruby 2.2.0dev (2014-04-11 trunk 45566) [x86_64-darwin13]
[ruby-dev:48103]

Description

Hash のキーに配列を使い、
キーが同じ値になるように以下のような操作をすると
Hash#each が無限ループします。

h = {} h[[]] = 1 # h #=> {[] => 1} h.keys[0] << 1 # h #=> {[1] => 1} h[[]] = 1 # h #=> {[1] => 1, [] => 1} h.keys[1] << 1 # h #=> {[1] => 1, [1] => 1} h.each { 1 } # 無限ループ 

このコードは、Ruby 1.9.3-p545 だと、無限ループにならないことを確認しています。

また、以下のような手順で {[1] => 1, [1] => 1} を作った場合は、無限ループになりませんでした。

h = {} h[[]] = 1 # h #=> {[] => 1} h[[1]] = 1 # h #=> {[] => 1} h.keys[0] << 1 # h #=> {[1] => 1, [1] => 1} h.each { 1 } # 無限ループにならない 

Related issues 1 (0 open1 closed)

Actions

Also available in: PDF Atom