0

什么是优雅的“红宝石”方式来改变每 N 次迭代循环的功能?我不想使用(1..50).each do |i|,因为我想遍历 list 中的每个对象objects

  objects.each do |object|
   #Do this with object information
   #Do not do this if this is the third time through the loop
  end
4

1 回答 1

5
objects.each_with_index do |object, idx|
   if idx == 2 # third time

   # or
   # if idx % 3 == 2 # every third time

     # do special thing
   else
     # do normal thing
   end
end
于 2013-02-13T14:30:06.877 回答