Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 551
Mark Szymanski Avatar asked Oct 24 '10 19:10

Mark Szymanski


People also ask

Is there a continue in Ruby?

The keyword next is the ruby equivalent of the continue keyword in other programming languages. The next keyword allows you to skip one iteration.

What is next keyword in Ruby?

Ruby Next Keyword (Skip Iteration) The next keyword allows you to skip one iteration. Example: Let's say you're counting strings. And for some reason you don't want to count strings with a size of 4.

What is for loop continue?

In a for loop, the continue keyword causes control to immediately jump to the update statement. In a while loop or do/while loop, control immediately jumps to the Boolean expression.


1 Answers

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  
like image 87
Ian Purton Avatar answered Oct 08 '22 22:10

Ian Purton