如何在ruby 1.8.7中对集合使用each_with_index
时指定从哪个索引开始?
collection.each_with_index do |element, index = 1|
#do smth
end
像这样使用它会产生以下错误:
syntax error, unexpected '=', expecting '|'
collection.each_with_index do |element, i = 1|
答案 0 :(得分:2)
试试这个:
collection[4..-1].each_with_index do |element, index|
#do smth
end
此示例将从第五个元素开始。