each_with_index {|(*args), idx| ... } -> Enumerator::LazyRuby 4.1 から[permalink][rdoc][edit]each_with_index -> Enumerator::Lazy-
with_index(0)と同じです。要素にインデックスを添えて遅延評価します。ブロックを指定しなかった場合は、要素とインデックスの組を生成する Enumerator::Lazy を返します。
ブロックを指定した場合は、各要素とインデックスでブロックを遅延評価し、元の要素をそのまま流す Enumerator::Lazy を返します。ブロックの返り値は使われません。返される Enumerator::Lazy のサイズは元と同じです。
例p (1..Float::INFINITY).lazy.each_with_index.first(2) # => [[1, 0], [2, 1]] p (1..Float::INFINITY).lazy.each_with_index.map { |x, i| x * i }.first(3) # => [0, 2, 6] p (1..Float::INFINITY).lazy.select(&:even?).each_with_index.first(2) # => [[2, 0], [4, 1]] p (1..3).lazy.each_with_index.size # => 3ブロックを指定した場合、ブロックは要素を取り出すときに評価されます。
例p (1..3).lazy.each_with_index { |x, i| print [x, i].inspect }.to_a # => [1, 0][2, 1][3, 2][1, 2, 3] p (1..3).lazy.each_with_index { |x, i| x * 10 }.to_a # => [1, 2, 3]Ruby 4.0 以前では、ブロックを指定すると Enumerable#each_with_index として即座にすべての要素を繰り返します(無限列では戻りません)。
[SEE_ALSO] Enumerator::Lazy#with_index, Enumerable#each_with_index