Changing the for loop counter in Ruby does not change the number of iterations.
You can change the counter variable, but this will only affect the current iteration:
> for k in 0...10 > k = 8 if k == 5 > puts k > end 0 1 2 3 4 8
The best way to achieve the desired behavior is to use the while suggested by @fotanus.
Of course, you can do this with a for loop using next statements, but this is much more ugly:
for k in 0...10 next if k > 5 && k <= 8 ... do stuff ... next if k == 5 ... do stuff ... end
source share