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
Share this Page URL
Help

Part I: The Basics > Choose the Right Control Structure

Chapter 2. Choose the Right Control Structure

If indentation, comments, and the form of your variable names are the things that give your program its look, then it’s the control structures—the ifs and elses and whiles—that bring it to life: It’s no accident that the generic name for a program is “logic.” Ruby includes a fairly familiar set of control structures, not much different than those available in more traditional languages. Look at any Ruby program and you will see the ifs, elses, and whiles of your programming youth. But if you look a little closer, you will also come across some odd-looking logical constructs, things with much less familiar names like unless and until.

In this chapter we will look at creating Ruby programs full of idiomatic control structures, at why sometimes you want to say if this then do that and at other times do this unless that. Along the way we will also explore Ruby’s take on what is true and what is false, and learn how you can avoid having your program logic take a wrong turn.


  

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