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?
Thanks in advance!
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?
Thanks in advance!
Inside for-loops and iterator methods like each
and map
the next
keyword in ruby will have the effect of jumping to the next iteration of the loop (same as continue
in C).
However what it actually does is just to return from the current block. So you can use it with any method that takes a block - even if it has nothing to do with iteration.
Yes, it's called next.
for i in 0..5
if i < 2 then
next
end
puts "Value of local variable is #{i}"
end
Just for information, exists redo
and retry
to iterations control. More information about this in Ruby Quicktips: http://rubyquicktips.tumblr.com/post/1122838559/redo-vs-retry