Ruby 1.8.7 each_with_index索引偏移量

时间:2012-05-30 12:05:48

标签: ruby-on-rails ruby ruby-enterprise-edition

如何在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|

1 个答案:

答案 0 :(得分:2)

试试这个:

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

此示例将从第五个元素开始。

相关问题