method

with_index

v1_9_1_378 - Show latest stable - Class: Enumerator
with_index()
public

Iterates the given block for each element with an index, which start from 0. If no block is given, returns an enumerator.

1Note

Minor correction to Rubybull's examples?

KimSJ ยท Mar 5, 2013

Was your first example intended to be: a=[11,22,31,224,44] => [11, 22, 31, 224, 44] a.each.with_index { |val,index| puts "index: #{index} for #{val}" }