Chapter 6. Control-flow techniques

 

This chapter covers

  • Conditional execution
  • Loops and looping techniques
  • Iterators
  • Exceptions and error handling

As you’ve already seen in the case of method calls—where control of the program jumps from the spot where the call is made to the body of the method definition—programs don’t run in a straight line. Instead, execution order is determined by a variety of rules and programming constructs collectively referred to as control-flow techniques.

Ruby’s control-flow techniques include the following:

  • Conditional execution—Execution depends on the truth of an expression.
  • Looping—A single segment of code is executed repeatedly.
  • Iteration—A call to a method is supplemented with a segment of code that the method can call one or more times during its own execution.
  • Exceptions—Error conditions are handled by special control-flow rules.

6.1. Conditional code execution

6.2. Repeating actions with loops

6.3. Iterators and code blocks

6.4. Error handling and exceptions

Summary

sitemap