Project

General

Profile

Actions

Bug #9729

closed

Hash#each が無限ループする

Added by takkanm (三村 益隆) about 10 years ago. Updated almost 10 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)

Is duplicate of Ruby master - Bug #9646: Infinite loop at Hash#eachClosed03/17/2014Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0