Ruby 2.3.0 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Enumeratorクラス > next
next -> object
[permalink][rdoc]「次」のオブジェクトを返します。
現在までの列挙状態に応じて「次」のオブジェクトを返し、列挙状態を1つ分進めます。列挙が既に最後へ到達している場合は、 StopIteration 例外を発生します。このとき列挙状態は変化しません。つまりもう一度 next を呼ぶと再び例外が発生します。
next メソッドによる外部列挙の状態は他のイテレータメソッドによる内部列挙には影響を与えません。ただし、 IO#each_line のようにおおもとの列挙メカニズムが副作用を伴っている場合には影響があり得ます。
[SEE_ALSO] Enumerator#rewind
str = "xyz"
enum = str.each_byte
str.bytesize.times do
puts enum.next
end
# => 120
# 121
# 122
str = "xyz"
enum = str.each_byte
begin
puts enum.next while true
rescue StopIteration
puts "iteration reached at end"
end
# => 120
# 121
# 122
# iteration reached at end
puts enum.next
#=> 再度 StopIteration 例外が発生
str = "xyz"
enum = str.each_byte
loop do
puts enum.next
end
# => 120
# 121
# 122