I can’t find any useful resources online that breaks down Ruby’s different flow-control options.
Let’s assume that I’m iterating over an array within in a method:
def a_method
things.each do |t|
# control options?
end
end
What are my different flow-control options here? How do they differ?
- retry
- return
- break
- next
- redo
retrycan be used inside a rescue block, to jump back into the begin block after the condition that caused the exception has been remedied. Inside a block it has the effect of jumping to the beginning of the yielding method. So inside each this means that retry will jump to the beginning of the loop.returnwill return from the method it’s inside of – in this case froma_method.breakwill return from the yielding method – in this case fromeach(which would be different from returning froma_methodif something happened between the end of the each-block and the end ofa_method).nextwill return from the block and thus jump to the next item inthings.redowill jump to the beginning of the block and thus repeat the current iteration.