method

with_index

with_index(p1 = v1)
public

Iterates the given block for each element with an index, which starts from offset. If no block is given, returns a new Enumerator that includes the index, starting from offset

offset

the starting index to use

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}" }