rubykeywordcontinue

Equivalent of "continue" in Ruby


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?


Solution

  • 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