Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

5.5. Altering Control Flow

In addition to conditionals, loops, and iterators, Ruby supports a number of statements that alter the flow-of-control in a Ruby program. These statements are:


return

Causes a method to exit and return a value to its caller.


break

Causes a loop (or iterator) to exit.


next

Causes a loop (or iterator) to skip the rest of the current iteration and move on to the next iteration.


redo

Restarts a loop or iterator from the beginning.


retry

Restarts an iterator, reevaluating the entire expression. The retry keyword can also be used in exception handling, as we’ll see later in the chapter.


throw/catch

A very general control structure that is named like and works like an exception propagation and handling mechanism. throw and catch are not Ruby’s primary exception mechanism (that would be raise and rescue, described later in this chapter). Instead, they are used as a kind of multilevel or labeled break.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial