Search code examples
ruby-on-railsrubyruby-enterprise-edition

Ruby 1.8.7 each_with_index index offset


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|

Solution

  • try this:

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

    this example will start from fifth element.