1

each_with_index在 ruby​​ 1.8.7 中的集合上使用时,如何指定从哪个索引开始?

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

像这样使用它会产生以下错误:

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

1 回答 1

2

试试这个:

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

这个例子将从第五个元素开始。

于 2012-05-30T12:08:30.373 回答