Equivalent of "continue" in Ruby

Mark Szymanski picture Mark Szymanski · Oct 24, 2010 · Viewed 250.7k times · Source

In C and many other languages, there is a continue keyword that, when used inside of a loop, jumps to the next iteration of the loop. Is there any equivalent of this continue keyword in Ruby?

Answer

Ian Purton picture Ian Purton · Oct 24, 2010

Yes, it's called next.

for i in 0..5
   if i < 2
     next
   end
   puts "Value of local variable is #{i}"
end

This outputs the following:

Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value of local variable is 5
 => 0..5