What is the Ruby equivalent to 'continue' from C?
next is the Ruby equivalent to continue.
An example:
3.upto(7) do |i|
next if i == 5
puts i
end
Output:
3
4
6
7
Ruby has other control keywords that you should be aware of:
break: Ends the loop, equivalent to same keyword in C
3.upto(7) do |i|
break if i == 5
puts i
end
Output:
3
4
redo: Re-executes the current iteration of the loop. Basically, it's the same as next except that the loop counter (or next element of an enumerable) is not incremented
3.upto(7) do |i|
puts i
redo if i == 5
end
Output:
3
4
4
4
.. (repeats forever)