Project

General

Profile

Actions

Bug #7493

closed

ブロックを渡された場合最初の1要素のみを返すEnumeratorに対してnextを送り続けると、2度目にStopIteration例外が発生する

Added by kachick (Kenichi Kamiya) over 11 years ago. Updated over 11 years ago.

Status:
Closed
Assignee:
-
Target version:
ruby -v:
ruby 1.8.7 (2012-10-12 patchlevel 371) [x86_64-linux]
[ruby-dev:46684]

Description

「ブロック付きで動かした場合、最初に見つかった1要素のみを返すメソッド」からEnumeratorを作ると、
2度目のnextでStopIteration例外を返すようです。
思い至った限りで確認したメソッドは、以下の物です。

  • Array#index
  • Array#find_index
  • Array#rindex
  • Enumerable#detect
  • Enumerable#find

これは、1.8.7と1.9間の仕様に於ける差異と考えた方が良いのでしょうか?
生成されたEnumeratorに対してto_aを送ると全要素を含むArrayが返る事から、
1.8.7に於いても全要素走査出来る方が自然では無いかと感じました。

to_aとの差異は、dbussinkの指摘で気づくことが出来ました。

https://github.com/rubinius/rubinius/pull/2063#issuecomment-10881875

array = [:a, :b, :c]
index_enum = array.index
index_enum.to_a #=> [:a, :b, :c]
index_enum.next #=> :a
index_enum.next #=> StopIteration: iteration reached at end


Files

CRuby1.8.7.rb (905 Bytes) CRuby1.8.7.rb CRuby-1.8.7 kachick (Kenichi Kamiya), 12/02/2012 01:35 PM
Other.rb (1007 Bytes) Other.rb Other kachick (Kenichi Kamiya), 12/02/2012 01:35 PM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0