Tải bản đầy đủ (.pdf) (443 trang)

1111222371 {d254d1f3} ruby programming ford 2010 10 22

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (6.93 MB, 443 trang )


1019763_FM_VOL-I.qxp

9/17/07

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
S 50
R 51

4:22 PM

Page viii


This page was intentionally left blank

1st Pass Pages


RUBY PROGRAMMING

JERRY LEE FORD, JR.

Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

C7656_fm.indd i

03/09/10 10:35 PM


Ruby Programming
Jerry Lee Ford, Jr.
Executive Editor: Marie Lee
Acquisitions Editor: Brandi Shailer
Senior Product Manager: Alyssa Pratt
Development Editor: Kent Williams
Content Project Manager: Lisa Weidenfeld
Editorial Assistant: Jacqueline Lacaire
Associate Marketing Manager: Shanna
Shelton
Cover image credit: ©istockphoto/ jennyking

Copyeditor: Suzanne Ciccone

© 2011 Course Technology, Cengage Learning
ALL RIGHTS RESERVED. No part of this work covered by the copyright
herein may be reproduced, transmitted, stored or used in any form or by
any means graphic, electronic, or mechanical, including but not limited
to photocopying, recording, scanning, digitizing, taping, Web distribution,
information networks, or information storage and retrieval systems, except
as permitted under Section 107 or 108 of the 1976 United States Copyright
Act, without the prior written permission of the publisher.
For product information and technology assistance, contact us at
Cengage Learning Customer & Sales Support, 1-800-354-9706
For permission to use material from this text or product,
submit all requests online at www.cengage.com/permissions
Further permissions questions can be emailed to


Proofreader: Kim Kosmatka
Indexer: Sharon Hilgenberg
Art Director: Faith Brosnan
Text Designer: Shawn Gersberger
Cover Designer: Cabbage Design Company
Compositor: Integra

Library of Congress Control Number: 2010935900
ISBN-13: 978-1-111-22237-6
ISBN-10: 1-111-22237-1
Course Technology
20 Channel Center Street
Boston, MA 02210

Cengage Learning is a leading provider of customized learning solutions with
office locations around the globe, including Singapore, the United Kingdom,
Australia, Mexico, Brazil, and Japan. Locate your local office at:
www.cengage.com/global
Cengage Learning products are represented in Canada by
Nelson Education, Ltd.
To learn more about Course Technology, visit www.cengage.com/
coursetechnology
To learn more about Cengage Learning, visit www.cengage.com
Purchase any of our products at your local college store or at our preferred
online store www.cengagebrain.com
Some of the product names and company names used in this book have been used
for identification purposes only and may be trademarks or registered trademarks
of their respective manufacturers and sellers.
Any fictional data related to persons or companies or URLs used throughout this
book is intended for instructional purposes only. At the time this book was printed,
any such data was fictional and not belonging to any real persons or companies.
Course Technology, a part of Cengage Learning, reserves the right to revise this
publication and make changes from time to time in its content without notice.
The programs in this book are for instructional purposes only. They have been
tested with care, but are not guaranteed for any particular intent beyond
educational purposes. The author and the publisher do not offer any warranties or
representations, nor do they accept any liabilities with respect to the programs.

Printed in the United States of America
1 2 3 4 5 6 7 15 14 13 12 11 10

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.


C7656_fm.indd ii

03/09/10 10:35 PM


This is an electronic version of the print textbook. Due to electronic rights
restrictions, some third party content may be suppressed. Editorial
review has deemed that any suppressed content does not materially
affect the overall learning experience. The publisher reserves the
right to remove content from this title at any time if subsequent
rights restrictions require it. For valuable information on pricing, previous
editions, changes to current editions, and alternate formats, please visit
www.cengage.com/highered to search by ISBN#, author, title, or keyword
for materials in your areas of interest.

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.


1019763_FM_VOL-I.qxp

9/17/07

1
2
3
4
5
6
7

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

38
39
40
41
42
43
44
45
46
47
48
49
S 50
R 51

4:22 PM

Page viii

This page was intentionally left blank

1st Pass Pages


Brief Contents
iii

Pref ace

. . . . . . . . . . . . . . . . . xii


CHAPTER 1

Ru by B as ic s

. . . . . . . . . . . . . . .

1

CHAPTER 2

In t er act in g wi th R uby

CHAPTER 3

Wo r k in g wi th Stri ngs, Obj ects,
an d Var iabl es . . . . . . . . . . . . . . . 77

CHAPTER 4

Im plem en ti ng Condi ti onal Logi c . . . . . . 114

CHAPTER 5

Wo r k in g wi th Loops . . . . . . . . . . . 146

CHAPTER 6

Wo r k in g wi th Col l ecti ons of Data


CHAPTER 7

Wo r k in g wi th R egul ar Ex pressi ons . . . . . 221

CHAPTER 8

Object -Or i ented Programmi ng . . . . . . . 257

CHAPTER 9

F ile an d F ol der Admi ni strati on . . . . . . . 294

CHAPTER 10

Debu g g in g . . . . . . . . . . . . . . . 331

CHAPTER 11

Ru by o n Rai l s Web Dev el opment . . . . . . 368

. . . . . . . . . . . 41

. . . . . 181

Glo s s ar y . . . . . . . . . . . . . . . . 401
In dex

. . . . . . . . . . . . . . . . . 407

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).

Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

C7656_fm.indd iii

03/09/10 10:35 PM


Contents
iv

Pref ace
CHAPTER 1

. . . . . . . . . . . . . . . . . xii

Ru by B as ics

. . . . . . . . . . . . . . .

Project Preview: The Ruby Joke Game . . . . . . . . . .
A Brief History of Computers and Computer Programming .
The Mechanized Era . . . . . . . . . . . . . . . . .
The First Generation . . . . . . . . . . . . . . . . .
The Second Generation . . . . . . . . . . . . . . . .
The Third Generation . . . . . . . . . . . . . . . . .
The Fourth Generation . . . . . . . . . . . . . . . .
The Fifth Generation . . . . . . . . . . . . . . . . .
1990 and Beyond . . . . . . . . . . . . . . . . . .
Introducing Ruby. . . . . . . . . . . . . . . . . . . .
Ruby Is Simple Yet Powerful. . . . . . . . . . . . . .

Ruby Is Object Oriented . . . . . . . . . . . . . . .
Ruby Is Extremely Flexible . . . . . . . . . . . . . .
Ruby Exists in Many Different Environments . . . . . .
Getting Ready to Work with Ruby . . . . . . . . . . . .
Determining Whether Ruby Is Already Installed . . . . .
Installing or Upgrading Ruby . . . . . . . . . . . . .
Working with Ruby . . . . . . . . . . . . . . . . . . .
Working at the Command Prompt . . . . . . . . . . .
IRB—Interactive Ruby . . . . . . . . . . . . . . . .
fxri–Interactive Ruby Help and Console . . . . . . . . .
Developing Ruby Programs . . . . . . . . . . . . . . .
Creating Ruby Programs on Microsoft Windows . . . . .
Creating Ruby Programs on Mac OS X . . . . . . . . .
Creating Ruby Programs on Linux and UNIX. . . . . . .
Using a Cross-Platform Ruby Editor . . . . . . . . . .
Creating Your First Ruby Program . . . . . . . . . . .
Running Your Ruby Program . . . . . . . . . . . . . .
Back to the Ruby Joke Game . . . . . . . . . . . . . .
Designing the Game . . . . . . . . . . . . . . . . .
Running Your New Ruby Program Game . . . . . . . .

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

1
. 2
. 5
. 5
. 6
. 6
. 7

. 8
. 9
. 9
. 10
. 11
. 13
. 14
. 14
. 15
. 15
. 17
. 21
. 21
. 21
. 23
. 24
. 24
. 24
. 24
. 24
. 25
. 26
. 27
. 27
. 32

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

C7656_fm.indd iv


03/09/10 10:35 PM


CONTENTS
Summing Up . . . . . .
Comprehension Check .
Reinforcement Exercises
Discovery Projects . . .
CHAPTER 2

In t er act in g wi th R uby

.
.
.
.

.
.
.
.

.
.
.
.

.
.

.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.

.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

. 35
. 36

. 38
. 39

. . . . . . . . . . . 41

Project Preview: The Ruby Tall Tale Game . . . .
Getting to Know IRB . . . . . . . . . . . . . .
Starting an IRB Session . . . . . . . . . . .
Working with Multiple IRB Sessions . . . . . .
Accessing IRB Online. . . . . . . . . . . . . .
Working with IRB. . . . . . . . . . . . . . . .
Executing Ruby Statements . . . . . . . . . .
Following Ruby’s Syntax Rules . . . . . . . . .
Executing Incomplete Statements . . . . . . .
Using IRB to Test Ruby Scripts . . . . . . . .
Using IRB to Perform Mathematical Calculations .
Using IRB as a Calculator . . . . . . . . . . .
Accessing Methods Stored in the Math Module .
Operator Precedence. . . . . . . . . . . . .
Overriding Operator Precedence . . . . . . .
Integers Versus Floating-Point Numbers . . . .
Accessing Ruby Documentation . . . . . . . . .
Back to the Ruby Tall Tale Game . . . . . . . .
Designing the Ruby Tall Tale Game. . . . . . .
Running Your New Ruby Script Game. . . . . .
Summing Up . . . . . . . . . . . . . . . . .
Comprehension Check . . . . . . . . . . . .
Reinforcement Exercises . . . . . . . . . . .
Discovery Projects . . . . . . . . . . . . . .
CHAPTER 3


.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

v

. 42
. 46
. 46
. 48
. 48
. 49
. 49
. 50
. 51
. 51
. 53
. 53
. 54
. 54
. 55
. 56
. 56

. 59
. 60
. 65
. 70
. 71
. 74
. 75

Wo r k in g wi th Stri ngs, Obj ects,
an d Var iabl es . . . . . . . . . . . . . . . 77
Project Preview: The Ruby Virtual Crazy
Creating and Formatting Text Strings .
Modifying Text String Format . . . .
String Interpolation. . . . . . . . .
Manipulating String Contents . . . . .
Concatenating Text Strings . . . . .
Multiplying Text Strings . . . . . . .
Comparing Text Strings. . . . . . .
Creating Multiline Text Strings . . . .

8-Ball
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .


Game
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .

.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

. 78
. 81
. 82
. 83
. 83
. 84
. 84
. 85
. 85


Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

C7656_fm.indd v

03/09/10 10:35 PM


CONTENTS
Working with String Class Methods . . . . . .
Object-Oriented Programming . . . . . . . . .
Defining a New Class . . . . . . . . . . . .
Defining Class Properties . . . . . . . . . .
Instantiating and Interacting with New Objects
Defining Class Methods. . . . . . . . . . .
Inheritance . . . . . . . . . . . . . . . .
Converting from One Class to Another . . . . .
Implicit Class Conversion . . . . . . . . . .
Explicit Class Conversion . . . . . . . . . .
Storing and Retrieving Data. . . . . . . . . .
Naming Variables . . . . . . . . . . . . .
Variable Assignments. . . . . . . . . . . .
Variable Scope . . . . . . . . . . . . . .
Storing Data That Does Not Change . . . . . .
Back to the Ruby Virtual Crazy 8-Ball Game . .
Designing the Game . . . . . . . . . . . .
Running Your New Ruby Script Game. . . . .
Summing Up . . . . . . . . . . . . . . . .
Comprehension Check . . . . . . . . . . .
Reinforcement Exercises . . . . . . . . . .

Discovery Projects . . . . . . . . . . . . .

vi

CHAPTER 4

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

. 87
. 88
. 88
. 89
. 89
. 90

. 91
. 92
. 93
. 93
. 95
. 96
. 96
. 97
. 98
100
100
107
107
108
111
112

Im plem en t in g Condi ti onal Logi c . . . . . . 114
Project Preview: The Ruby Typing Challenge Game
Using Conditional Logic to Create Adaptive
Scripts . . . . . . . . . . . . . . . . . . .
Performing Alternative Types of Comparisons . .
Conditional Logic Modifiers . . . . . . . . . . .
The if Modifier . . . . . . . . . . . . . . .
The unless Modifier . . . . . . . . . . . . .
Working with if and unless Expressions. . . . .
Creating if Expressions . . . . . . . . . . .
Replacing if Modifiers with if Expressions . .
Creating unless Expressions . . . . . . . . .
Using case Blocks to Analyze Data . . . . . . .

Using the Ternary Operator . . . . . . . . . . .
Nesting Conditional Statements . . . . . . . . .
Combining and Negating Logical Comparison
Operations . . . . . . . . . . . . . . . . .
Back to the Ruby Typing Challenge Game . . . .
Designing the Game . . . . . . . . . . . . .
Running Your New Ruby Script Game. . . . . .

. . . . . 115
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.

119
121
122
122
122
123
123
124
126
127
129
129

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

130
132
132
140

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.


C7656_fm.indd vi

03/09/10 10:35 PM


CONTENTS
Summing Up . . . . . .
Comprehension Check .
Reinforcement Exercises
Discovery Projects . . .
CHAPTER 5

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

140
141
144

145

Wo r k in g wi th Loops . . . . . . . . . . . 146
Project Preview: The Superman Movie Trivia Quiz
Understanding Loops . . . . . . . . . . . .
Using Language Constructs to Create Loops . .
Working with while Loops . . . . . . . . .
Working with until Loops . . . . . . . . .
Working with for...in Loops . . . . . . .
Using Loop Modifiers . . . . . . . . . . . .
The while Modifier . . . . . . . . . . . .
The until Modifier . . . . . . . . . . . .
Executing Looping Methods. . . . . . . . . .
Working with the each Method . . . . . . .
Working with the times Method . . . . . . .
Working with the upto Method . . . . . . .
Working with the downto Method . . . . . .
Working with the step Method . . . . . . .
Working with the loop Method . . . . . . .
Altering Loop Execution . . . . . . . . . . .
Prematurely Terminating Loop Execution . . .
Repeating the Current Execution of a Loop . .
Skipping to the Next Iteration of a Loop . . .
Restarting a Loop from the Beginning . . . .
Back to the Superman Movie Trivia Quiz . . . .
Designing the Game . . . . . . . . . . . .
Running Your New Ruby Script Game. . . . .
Summing Up . . . . . . . . . . . . . . . .
Comprehension Check . . . . . . . . . . .
Reinforcement Exercises . . . . . . . . . .

Discovery Projects . . . . . . . . . . . . .

CHAPTER 6

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

147
150
151
151
152
153
154
154
155
156
156
157
158
159
160
161
162
162
163
163
164
165
165
174
174
175
178

179

Wo r k in g wi th Col l ecti ons of Data

. . . . . 181

Project Preview: The Ruby Number Guessing
Storing and Manipulating Lists Using Arrays
Managing Data Using Arrays . . . . . .
Replacing and Adding Array Items . . . .
Determining if an Array Is Empty . . . .
Retrieving Items from an Array . . . . .

Game
. . .
. . .
. . .
. . .
. . .

.
.
.
.
.
.

.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

vii

182
186
186

188
190
190

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

C7656_fm.indd vii

03/09/10 10:35 PM


CONTENTS
Using a Loop to Iterate Through the Contents
of an Array. . . . . . . . . . . . . . . . . .
Deleting Items from an Array . . . . . . . . .
Sorting the Contents of an Array Alphabetically .
Sorting Array Contents in Reverse
Alphabetical Order . . . . . . . . . . . . .
Searching an Array . . . . . . . . . . . . . .
Storing and Manipulating Lists Using Hashes . . .
Creating a Hash . . . . . . . . . . . . . . .
Adding a Hash’s Key-Value Pairs . . . . . . . .
Deleting a Hash’s Key-Value Pairs . . . . . . .
Determining the Number of Key-Value Pairs
in a Hash . . . . . . . . . . . . . . . . .
Retrieving Data Stored in Hashes . . . . . . .
Sorting Hash Keys . . . . . . . . . . . . . .
Back to the Ruby Number Guessing Game . . . .
Designing the Game . . . . . . . . . . . . .

Running Your New Ruby Script Game. . . . . .
Summing Up . . . . . . . . . . . . . . . . .
Comprehension Check . . . . . . . . . . . .
Reinforcement Exercises . . . . . . . . . . .
Discovery Projects . . . . . . . . . . . . . .

viii

CHAPTER 7

. . . . . . 192
. . . . . 192
. . . . . 194
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.

.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

194
195
196
196
198
199

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

200
201
202
204
204
212
212
214
217
219


Wo r k in g w it h R egul ar Ex pressi ons . . . . . 221
Project Preview: The Ruby Word Guessing Game .
Using Regular Expressions for Data Validation . .
Matching Basic Patterns . . . . . . . . . . . .
Matching Specific Patterns . . . . . . . . . .
Matching Multiple Patterns . . . . . . . . . .
Working with Metacharacters . . . . . . . . . .
Escaping Metacharacters . . . . . . . . . . .
Matching Individual Characters . . . . . . . .
Matching a Pattern at the Beginning of a String .
Matching a Pattern at the End of a String . . .
Matching Once or Not at All . . . . . . . . . .
Matching Zero or More Times . . . . . . . . .
Matching Any of a Collection of Characters . . .
Other Uses for Regular Expressions . . . . . . .
Overcoming Differences in Case . . . . . . . .
String Substitution . . . . . . . . . . . . . .
Back to the Ruby Word Guessing Game . . . . .
Designing the Game . . . . . . . . . . . . .
Running Your New Ruby Script Game. . . . . .

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.

222
227
228
228
228
230
231
231
231
232
232
232
233
233
234
234
236
236
249

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

C7656_fm.indd viii

03/09/10 10:35 PM



CONTENTS
Summing Up . . . . . .
Comprehension Check .
Reinforcement Exercises
Discovery Projects . . .
CHAPTER 8

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.

.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

249
251
254
255

Object -Or i ented Programmi ng . . . . . . . 257
Project Preview: The Rock, Paper, Scissors Game
Key Features of Object-Oriented Programming . .

Abstraction . . . . . . . . . . . . . . . . .
Encapsulation . . . . . . . . . . . . . . . .
Inheritance . . . . . . . . . . . . . . . . .
Polymorphism . . . . . . . . . . . . . . . .
Initializing Objects Upon Instantiation . . . . . .
Understanding Variable Scope . . . . . . . . .
Working with Local Variables . . . . . . . . .
Working with Global Variables . . . . . . . . .
Working with Instance Variables . . . . . . . .
Working with Class Variables . . . . . . . . .
Taking Advantage of Ruby’s Built-in Classes . . .
Modifying Ruby Classes . . . . . . . . . . . .
Back to the Rock, Paper, Scissors Game . . . .
Designing the Game . . . . . . . . . . . . .
Running Your New Ruby Program Game . . . .
Summing Up . . . . . . . . . . . . . . . . .
Comprehension Check . . . . . . . . . . . .
Reinforcement Exercises . . . . . . . . . . .
Discovery Projects . . . . . . . . . . . . . .

CHAPTER 9

.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

ix

258
262
262
263
266
267
269
271
271
271
273

273
274
275
277
277
286
286
287
290
292

F ile an d F ol der Admi ni strati on . . . . . . . 294
Project Preview: The Ruby Blackjack Game . .
Redirecting File Input and Output . . . . . .
Administering Text Files and Folders . . . . .
Verifying Whether a File or Folder Exists . .
Retrieving Information About a File or Folder
Creating New Folders . . . . . . . . . .
Deleting Files and Folders . . . . . . . .
Renaming Files . . . . . . . . . . . . .
Working with Files and Folders on Different
Operating Systems . . . . . . . . . . . .
Reading From and Writing to Text Files. . . .
Writing Data to Text Files . . . . . . . . .
Appending Data to the End of Text Files . .
Reading Data from Text Files . . . . . . .

.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.


295
299
300
300
301
303
304
304

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.

.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

305
306
307
308
309

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.


C7656_fm.indd ix

03/09/10 10:35 PM


CONTENTS
Back to the Ruby Blackjack Game . . .
Designing the Game . . . . . . . .
Running Your New Ruby Script Game.
Summing Up . . . . . . . . . . . .
Comprehension Check . . . . . . .
Reinforcement Exercises . . . . . .
Discovery Projects . . . . . . . . .

x

CHAPTER 10

.
.
.
.
.
.
.

.
.
.
.

.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.


311
311
321
322
323
326
329

Debu g g in g . . . . . . . . . . . . . . . 331
Project Preview: The Ruby Tic-Tac-Toe Game
Types of Script Errors . . . . . . . . . .
Syntax Errors . . . . . . . . . . . . .
Runtime Errors . . . . . . . . . . . .
Logical Errors . . . . . . . . . . . . .
Handling Exceptions . . . . . . . . . . .
Creating Exception Handlers . . . . . .
Accessing Error Information . . . . . .
Handling Different Types of Errors . . . .
Retrying Failed Statements . . . . . . .
Tracking a Script’s Logical Flow . . . . . .
Finding Bugs Using the Ruby Debugger . .
Starting the Debugger . . . . . . . . .
Back to the Ruby Tic-Tac-Toe Game . . . .
Designing the Game . . . . . . . . . .
Running Your New Ruby Script Game. . .
Summing Up . . . . . . . . . . . . . .
Comprehension Check . . . . . . . . .
Reinforcement Exercises . . . . . . . .
Discovery Projects . . . . . . . . . . .


CHAPTER 11

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

332
335

336
336
337
337
337
338
339
339
341
342
342
347
347
359
360
361
364
366

Ru by o n Rails Web Dev el opment . . . . . . 368
Project Preview: The Blog Application . . . . . . . .
Overview of Ruby on Rails . . . . . . . . . . . . .
Skills Needed to Work with Ruby on Rails. . . . . .
Required Software . . . . . . . . . . . . . . . .
Development Philosophy . . . . . . . . . . . . .
Architecture . . . . . . . . . . . . . . . . . . .
Working with Database Management Systems . . .
Getting Ruby on Rails Up and Running . . . . . . . .
Installing Ruby On Rails . . . . . . . . . . . . . .
Selecting a Web Server . . . . . . . . . . . . . .

Installing the SQLite Database Management System .

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.

.
.
.
.

.
.
.
.
.
.
.

369
369
371
371
371
372
374
375
375
377
377

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

C7656_fm.indd x

03/09/10 10:35 PM


CONTENTS
Verifying Ruby on Rails Setup . . . . . . . .

Creating a Demo Application . . . . . . .
Verifying the Execution of Your Web Server .
Ruby on Rails Applications Files . . . . . .
Back to the Blog Application . . . . . . . .
Design the Blog Application . . . . . . . .
Running Your New Ruby on Rails Project . .
Learning More About Ruby on Rails . . . . .
Summing Up . . . . . . . . . . . . . . .
Comprehension Check . . . . . . . . . .
Reinforcement Exercises . . . . . . . . .
Discovery Projects . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.

.
.
.

.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.


.
.
.
.
.
.
.
.
.
.
.
.

377
378
379
381
384
384
386
387
388
390
393
400

xi

Glo s s ar y . . . . . . . . . . . . . . . . 401
In dex


. . . . . . . . . . . . . . . . . 407

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

C7656_fm.indd xi

03/09/10 10:35 PM


Preface
xii

Ruby is an object-oriented, interpreted programming language. It is
object-oriented in that it views (and interacts with) everything as an
object. Ruby is interpreted in that its programs are compiled and executed by an interpreter at runtime. This allows you to make changes
to your Ruby programs and then immediately run the programs to
see how they work, without having to go through the extensive compilation that is required by many other programming languages.
Ruby is a cross-platform programming language, which means that
its programs can run on Windows, Mac OS X, UNIX, Linux, and
many other operating systems. This allows you to leverage your Ruby
programming skills, maximizing the investment you’ve made in
learning Ruby. You can even apply your Ruby programming skills to
the development of web applications, using a specialized Ruby framework known as Ruby on Rails.
Ruby is easy to learn, yet quite powerful. This makes it an excellent
choice for first-time programmers. It is a great programming language
for tackling all kinds of small tasks. At the same time, Ruby is powerful
enough to support the development of the most complicated programming tasks, making it a great language for experienced programmers
looking for an advanced object-oriented programming language.

Best of all, Ruby is free. If your computer is running on a UNIX or
Linux operating system, Ruby may already be installed. If not, you
can download and install it directly from the Internet. Free installation packages are available for Microsoft Windows users as well. This
book is dedicated to teaching you what you need to know to begin
programming with Ruby. By the time you have finished this book,
you will have developed a good understanding of the fundamentals of
Ruby programming and will have a solid foundation to build upon in
becoming an effective Ruby programmer.

The Approach
This book uses various instructional techniques to teach you how
to develop Ruby programs. Each chapter guides you through the
creation of a Ruby program using concepts learned in the chapter.

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

C7656_fm.indd xii

03/09/10 10:35 PM


P R E FA C E

And each chapter ends with both a Summing Up section and a set of
Reinforcement Exercises designed to give you hands-on experience
with the programming techniques and concepts covered in that
chapter.
In addition, each chapter includes a series of Discovery Projects
designed to further your understanding of important topics and

concepts. And each chapter contains two short quizzes as well as a
Comprehension Check designed to measure your understanding of
the concepts and your mastery of the material.

xiii

Overview of This Book
This book consists of eleven chapters, each of which focuses on a
specific aspect of Ruby programming. Here is an outline of the book’s
contents:
Chapter 1, “Ruby Basics.” An overview of Ruby program-

ming, including information about its history, major features, and
capabilities.
Chapter 2, “Interacting with Ruby.” How to interact with Ruby

from the command line. How to use the interactive Ruby shell known
as IRB. Here, you begin to learn about Ruby’s syntax and its built-in
support for object-oriented programming.
Chapter 3, “Working with Strings, Objects, and Variables.” How
to work with and manipulate string and numeric data. How to define
variables and objects and use them to store and retrieve data.
Chapter 4, “Implementing Conditional Logic.” How to use con-

ditional logic as a tool for analyzing data and controlling the logical
execution of script statements.
Chapter 5, “Working with Loops.” How to formulate and control

the execution of loops. How to create Ruby programs that are capable
of processing enormous amounts of data or performing repetitive

tasks.
Chapter 6, “Working with Collections of Data.” How to store and

process related collections of data more efficiently. How to store data
in indexed lists that can then be processed efficiently using loops.
How to define data in hashes, which provide an efficient means of
storing large collections of data using key-value pairs.
Chapter 7, “Working with Regular Expressions.” How to use regu-

lar expressions to evaluate and manipulate strings. This lays the foundation for dissecting user input and file contents.

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

C7656_fm.indd xiii

03/09/10 10:35 PM


P R E FA C E
Chapter 8, “Object-Oriented Programming.” Here, you will learn

more about Ruby’s support for object-oriented programming, including the concepts of encapsulation, polymorphism, and inheritance.
Chapter 9, “File and Folder Administration.” How to access and
manage file-system resources. How to read from and write to files.
xiv

Chapter 10, “Debugging.” How to fix program errors. How to

develop exception handlers that trap and deal with errors. How to

work with Ruby’s integrated debugger to exercise detailed control
over the execution of your Ruby scripts.
Chapter 11, “Ruby on Rails Web Development.” An overview of

Ruby on Rails and the technologies required to work with it. How to
set up your own personal web server and database and how to use
Ruby on Rails to develop web applications.

Features
To facilitate learning, Ruby Programming includes the following
features:
CHAPTER OBJECTIVES Each chapter begins with a list of the

important concepts to be learned. This is both a quick reference to
the chapter’s contents and a useful study guide.
ILLUSTRATIONS AND TABLES Illustrations help you visualize

common components and relationships. Tables present conceptual
items and examples in a readable format.
POINTERS These provide you with practical advice and
proven strategies related to the concept being discussed.
FACTS These provide additional helpful information on
specific techniques and concepts.
CAREFUL These point out troublesome issues you need to
watch out for when writing Ruby programs.
SHORT QUIZZES At the end of each major topic, these quick

comprehension checks assess your understanding of the section’s
material.
SUMMING UP These brief overviews of a chapter’s contents provide


a helpful way to recap and revisit the ideas covered in each chapter.
COMPREHENSION CHECKS These sets of 20 review questions rein-

force the main ideas introduced in each chapter and help you determine how well you understand the concepts covered in the chapter.

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

C7656_fm.indd xiv

03/09/10 10:35 PM


P R E FA C E
REINFORCEMENT EXERCISES Although it is important to under-

stand the concepts behind Ruby programming, no amount of theory
can improve on applied knowledge. Toward this end, each chapter
provides a set of exercises for each major topic.
DISCOVERY PROJECTS These give you further opportunities to apply what you have learned or expand upon your
understanding of Ruby programming.

xv

Instructor Resources
The following supplemental materials are available when this book is
used in a classroom setting. All the resources available with this book
are provided to the instructor on a CD.
ELECTRONIC INSTRUCTOR’S MANUAL The Instructor’s Manual that

accompanies this textbook provides additional instructional material
to assist in class preparation, including Syllabi, Chapter Outlines, and
Teaching Tips.
EXAMVIEW® This textbook is accompanied by ExamView, a powerful
testing software package that allows instructors to create and administer printed, computer (LAN-based), and Internet exams. ExamView
includes hundreds of questions that correspond to the topics covered in this text, enabling students to generate detailed study guides
that include page references for further review. The computer-based
and Internet testing components allow students to take exams at
their computers, and save the instructor time by grading each exam
automatically.
POWERPOINT PRESENTATIONS Microsoft PowerPoint slides are
provided for each chapter. These are meant as a teaching aid for classroom presentation and can be made available to students or printed
for classroom distribution. Instructors can add their own slides for
additional topics they present to the class.
DATA FILES Files that contain all the data necessary for completing
the book’s Reinforcement Exercises and Discovery Projects are provided through the Course Technology website at www.cengage.com/
coursetechnology. They are also available on the Instructor’s Resource
CD and for student download at cengagebrain.com.
SOLUTION FILES Solutions to the end-of-chapter Comprehension

Checks, the Reinforcement Exercises, and the Discovery Projects are
provided on the Instructor Resources CD and through the Course
Technology website at www.cengage.com/coursetechnology. The solutions are password protected.

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

C7656_fm.indd xv

03/09/10 10:36 PM



P R E FA C E
DISTANCE LEARNING Course Technology is proud to present online

xvi

test banks in WebCT and Blackboard, to provide the most complete
and dynamic learning experience possible. Instructors are encouraged
to make the most of the course, both online and offline. For more
information on how to access your online test bank, contact your
local Course Technology sales representative.

Acknowledgements
Ruby Programming represents the time, effort, and hard work of
numerous individuals to whom I would be remiss if I did not offer
thanks. First and foremost, I need to thank this book’s Development
Editor, Kent Williams, for his expert guidance and advice and
for greatly improving the quality and presentation of this text.
Special thanks also needs to be given to Ann Shaffer, Consulting
Development Editor, Alyssa Pratt, Senior Product Manager, Amy
Jollymore, Acquisitions Editor, and Lisa Weidenfeld, Content Project
Manager, for all their help in making this book a reality.
Recognition must also be made to the reviewers who provided their
time, talent, and advice throughout the development of this book:
Diane DelMonte, Briarcliffe College, Sue Fitzgerald, Metropolitan
State University, and Jim Innis, North Central Texas College.
Last but by no means least, I must thank my wonderful children,
Alexander, William, and Molly, and my beautiful wife, Mary, for their
patience and support during the duration of my work on this book.


Requirements for Completing this Book
To reproduce the examples presented in this book and to complete
end-of-chapter program development projects, you will need a
computer running Microsoft Windows, Mac OS X, or one of the
many versions of Linux or UNIX. You can use a computer in your
school lab or your own computer. To use your own computer, you
will need the following development resources, which you can
obtain for free:
• Ruby, which can be downloaded and installed for free at http://
www.ruby-lang.org.
• A program or text editor. On Windows, you can use the Notepad
application. On Mac OS X, you can use TextEdit, provided you
configure it to save out as plain text. On UNIX and Linux, you
can use vi or any other text editor that may be installed. A wordprocessing program will not work, as it inserts formatting information into the document that will cause your scripts to fail.

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

C7656_fm.indd xvi

03/09/10 10:36 PM


P R E FA C E

• RubyGems. A Ruby package that facilitates the installation of
Ruby on Rails and related components. RubyGems is automatically
installed with Ruby on Microsoft Windows. Users of other operating systems can obtain RubyGems at />download.
• Ruby on Rails. An installation program for Ruby on Rails can be

downloaded using RubyGems.

xvii

• A web browser, such as Microsoft Internet Explorer or later or
Mozilla Firefox to support Ruby on Rails testing and execution.
• A web server to host Ruby on Rails applications and facilitate
application execution and testing. Ruby on Rails can work with any
web server. The WEBrick web server is automatically installed as
a part of Ruby on Rails standard installation and is the web server
used in this book.
• A database to store application data for Ruby on Rails applications. Ruby on Rails can work with many different types of
database systems. By default, Ruby on Rails applications are automatically configured to work with an SQLite database. You download SQLite for free using RubyGems.

To the Instructor
To complete some of the exercises in this book, your students must
work with a set of data files. You can obtain the data files through the
Course Technology website at www.cengage.com/coursetechnology.

Course Technology Data Files
You are granted a license to copy the data files to any computer or
computer network used by people who have purchased this book.

Visit Our Web Site
Additional materials designed especially for this book might be
available for your course. Periodically search www.cengage.com/
coursetechnology for more information.

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.


C7656_fm.indd xvii

03/09/10 10:36 PM


1019763_FM_VOL-I.qxp

9/17/07

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
S 50

R 51

4:22 PM

Page viii

This page was intentionally left blank

1st Pass Pages


CHAPTER

Ruby Basics

1

In this chapter, you:
 Get a brief history of computer programming
 Get an introduction to Ruby
 Get ready to work with Ruby
 Use Ruby interactively
 Develop Ruby programs
 Create the Ruby Joke game

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

C7656_ch01.indd 1


8/30/10 7:55:57 PM


CHAPTER 1

2

Ruby Basics

Ruby is a programming language developed in 1993 to run on
UNIX. However, it has since been adapted to run on many
other popular operating systems, including Microsoft Windows,
Mac OS X, and Linux. Ruby is distributed under an open-source
license, allowing anyone to install and use it for free. In this chapter,
you learn background information required to begin creating and
executing Ruby programs. You also learn how to use Ruby to create
the first of a number of computer games presented in this book.

Project Preview: The Ruby Joke Game
In this chapter and in each chapter that follows, you learn how to
create a new computer game. By following along and creating your
own copies of these games, you gain practical, hands-on experience
programming with Ruby, and you develop a foundation upon which
you can develop larger and more complex Ruby projects, such as
system and network administration programs or website applications.
In this chapter’s game project, the Ruby Joke game, you learn the
basic steps involved in writing a Ruby program. When the game
begins, the screen shown in Figure 1-1 displays, prompting the player
for permission to begin telling jokes.


Figure 1-1

The player may elect to play the game or quit

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

C7656_ch01.indd 2

8/30/10 7:55:57 PM


Project Preview: The Ruby Joke Game

If the player enters a value of n, the game responds by displaying
a message that invites the player to return to play another time.
However, if the player enters a value of y, the game responds by
displaying the first of a series of jokes, as demonstrated in Figure 1-2.
3

Figure 1-2
game play

The player must press Enter to advance from screen to screen during

In order to view the first joke’s punch line, the player must press
Enter, after which the screen shown in Figure 1-3 displays.

Figure 1-3


The punch line for the game’s first joke

To view successive jokes, the player must continue to press Enter.
Once the game’s final joke has been told, the screen shown in
Figure 1-4 displays, thanking the player for playing the game.

Copyright 2010 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.

C7656_ch01.indd 3

8/30/10 7:55:57 PM


×