Tip: Ruby Break, Redo, Next and Retry
Posted by anthony crumley, Sat Dec 15 09:16:00 UTC 2007
Ruby has some interesting loop flow control statements. These work with while, until, and for loops as well as iterators. For some reason, I find it very interesting that they work with iterators.
Break simply breaks out of the most immediate loop and resumes with the next statement after the loop. It is like Bobby Petrino leaving the Atlanta Falcons to coach at Arkansas. Just stop whatever is going on and move to the next thing without looking back.
Redo repeats the current iteration of the loop without rechecking the condition. It is like a do over or mulligan.
Next skips to the end of the current iteration and begins the next one normally. It is like the Soup Nazi, “No soup for you! Come back, one year. Next!!”
Retry starts the whole loop over again from the beginning. It is like the movie Groundhog Day. You can just keep repeating it until you get it right.