HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyCritical

Equivalent of "continue" in Ruby

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
equivalentcontinueruby

Problem

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

Code Snippets

for i in 0..5
   if i < 2
     next
   end
   puts "Value of local variable is #{i}"
end
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

Context

Stack Overflow Q#4010039, score: 1090

Revisions (0)

No revisions yet.