CommonThread - Our Blog

Tip: Ruby Break, Redo, Next and Retry

Written By: Anthony Crumley

December 15th, 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.

3 Responses to “Tip: Ruby Break, Redo, Next and Retry”

  1. dave Says:
    Thanks! Exactly what i needed.. as a newbie
  2. Ric Says:
    Thanks. Memorable explanations!
  3. Guitaronin Says:
    Well said! And helpful - thanks!

Leave a Reply