snippetrubyCritical
In Ruby, how do I skip a loop in a .each loop, similar to 'continue'
Viewed 0 times
howsimilarcontinuerubyeachloopskip
Problem
In Ruby, how do I skip a loop in a
.each loop, similar to continue in other languages?Solution
Use
prints:
For additional coolness check out also
Works also for friends like
For more info see http://ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html#UL.
next: (1..10).each do |a|
next if a.even?
puts a
endprints:
1
3
5
7
9For additional coolness check out also
redo and retry.Works also for friends like
times, upto, downto, each_with_index, select, map and other iterators (and more generally blocks).For more info see http://ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html#UL.
Code Snippets
(1..10).each do |a|
next if a.even?
puts a
end1
3
5
7
9Context
Stack Overflow Q#4230322, score: 692
Revisions (0)
No revisions yet.