Ruby 1.8.7 Reference Manual > All Libraries > Builtin Library > module Enumerable > each_with_index

instance method Enumerable#each_with_index

each_with_index -> Enumerable::Enumerator
enum_with_index -> Enumerable::Enumerator
each_with_index {|item, index| ... } -> self

要素とそのインデックスをブロックに渡して繰り返します。

self を返します。

例:

[5, 10, 15].each_with_index do |n, idx|
  p [n, idx]
end
    # => [5, 0]
    #    [10, 1]
    #    [15, 2]