Ruby 1.8.7 each_with_index index offset

573 views Asked by At

How can i specify which index to start from when using each_with_index on a collection in ruby 1.8.7?

collection.each_with_index do |element, index = 1|
  #do smth
end

Using it like this gives the following error:

syntax error, unexpected '=', expecting '|'
collection.each_with_index do |element, i = 1|
1

There are 1 answers

4
alexkv On BEST ANSWER

try this:

collection[4..-1].each_with_index do |element, index|
  #do smth
end

this example will start from fifth element.