About the Authors |
|
xix | |
About the Technical Reviewer |
|
xxi | |
Foreword |
|
xxiii | |
Acknowledgments |
|
xxvii | |
Introduction |
|
xxix | |
|
Part I Foundations and Scaffolding |
|
|
1 | (138) |
|
Chapter 1 Let's Get It Started: Installing Ruby |
|
|
3 | (10) |
|
|
4 | (7) |
|
|
4 | (2) |
|
|
6 | (1) |
|
|
7 | (3) |
|
|
10 | (1) |
|
|
11 | (2) |
|
Chapter 2 Programming == Joy: A Whistle-Stop Tour of Ruby and Object Orientation |
|
|
13 | (24) |
|
|
14 | (5) |
|
|
14 | (1) |
|
Ruby Is "English for Computers" |
|
|
15 | (1) |
|
Why Ruby Makes a Great Programming Language |
|
|
16 | (1) |
|
|
17 | (2) |
|
Turning Ideas into Ruby Code |
|
|
19 | (9) |
|
How Ruby Understands Concepts with Objects and Classes |
|
|
20 | (1) |
|
|
20 | (3) |
|
|
23 | (1) |
|
|
23 | (5) |
|
|
28 | (6) |
|
|
29 | (1) |
|
|
30 | (2) |
|
Using the Methods of the String Class |
|
|
32 | (2) |
|
Using Ruby in a Non-object-Oriented Style |
|
|
34 | (1) |
|
|
35 | (2) |
|
Chapter 3 Ruby's Building Blocks: Data, Expressions, and Flow Control |
|
|
37 | (58) |
|
|
37 | (10) |
|
|
38 | (1) |
|
|
38 | (3) |
|
Comparison Operators and Expressions |
|
|
41 | (2) |
|
Looping Through Numbers with Blocks and Iterators |
|
|
43 | (2) |
|
|
45 | (2) |
|
|
47 | (1) |
|
|
47 | (14) |
|
|
47 | (2) |
|
|
49 | (2) |
|
|
51 | (2) |
|
|
53 | (1) |
|
Regular Expressions and String Manipulation |
|
|
54 | (7) |
|
|
61 | (8) |
|
|
61 | (3) |
|
Splitting Strings into Arrays |
|
|
64 | (1) |
|
|
65 | (1) |
|
|
66 | (3) |
|
|
69 | (4) |
|
|
70 | (2) |
|
|
72 | (1) |
|
|
73 | (10) |
|
|
73 | (2) |
|
|
75 | (1) |
|
|
76 | (2) |
|
|
78 | (1) |
|
|
79 | (1) |
|
|
80 | (3) |
|
Other Useful Building Blocks |
|
|
83 | (8) |
|
|
83 | (4) |
|
|
87 | (1) |
|
|
88 | (2) |
|
Converting Objects to Other Classes |
|
|
90 | (1) |
|
|
91 | (4) |
|
Chapter 4 Developing Your First Ruby Application |
|
|
95 | (28) |
|
Working with Source Code Files |
|
|
95 | (5) |
|
|
96 | (1) |
|
A Simple Source Code File |
|
|
97 | (1) |
|
|
98 | (2) |
|
Our Application: A Text Analyzer |
|
|
100 | (12) |
|
|
101 | (1) |
|
Building the Basic Application |
|
|
102 | (1) |
|
Obtaining Some Dummy Text |
|
|
102 | (1) |
|
Loading Text Files and Counting Lines |
|
|
103 | (2) |
|
|
105 | (1) |
|
|
106 | (2) |
|
Counting Sentences and Paragraphs |
|
|
108 | (2) |
|
|
110 | (1) |
|
|
111 | (1) |
|
|
112 | (7) |
|
Percentage of "Useful" Words |
|
|
112 | (3) |
|
Summarizing by Finding "Interesting" Sentences |
|
|
115 | (3) |
|
Analyzing Files Other Than text.txt |
|
|
118 | (1) |
|
|
119 | (3) |
|
|
122 | (1) |
|
Chapter 5 The Ruby Ecosystem |
|
|
123 | (16) |
|
|
124 | (5) |
|
The Land of the Rising Sun |
|
|
124 | (1) |
|
|
125 | (1) |
|
|
126 | (3) |
|
Alternative Ruby Implementations |
|
|
129 | (1) |
|
|
129 | (3) |
|
Why Rails Came into Existence |
|
|
130 | (1) |
|
How the Web (2.0) Was Won |
|
|
131 | (1) |
|
|
132 | (1) |
|
|
132 | (1) |
|
Where and How to Get Help |
|
|
133 | (2) |
|
|
133 | (1) |
|
|
134 | (1) |
|
|
134 | (1) |
|
|
135 | (1) |
|
|
135 | (2) |
|
|
135 | (1) |
|
|
136 | (1) |
|
|
137 | (1) |
|
|
137 | (2) |
|
|
139 | (252) |
|
Chapter 6 Classes, Objects, and Modules |
|
|
141 | (56) |
|
Why Use Object Orientation? |
|
|
141 | (4) |
|
Object Orientation Basics |
|
|
145 | (25) |
|
Local, Global, Object, and Class Variables |
|
|
146 | (5) |
|
Class Methods vs. Instance Methods |
|
|
151 | (2) |
|
|
153 | (4) |
|
Overriding Existing Methods |
|
|
157 | (2) |
|
Reflection and Discovering an Object's Methods |
|
|
159 | (2) |
|
|
161 | (5) |
|
|
166 | (2) |
|
|
168 | (1) |
|
|
169 | (1) |
|
Modules, Namespaces, and Mix-ins |
|
|
170 | (14) |
|
|
171 | (3) |
|
|
174 | (10) |
|
Building a Dungeon Text Adventure with Objects |
|
|
184 | (11) |
|
|
184 | (1) |
|
Creating the Initial Classes |
|
|
185 | (1) |
|
Structs: Quick and Easy Data Classes |
|
|
186 | (3) |
|
|
189 | (1) |
|
|
190 | (5) |
|
|
195 | (2) |
|
Chapter 7 Projects and Libraries |
|
|
197 | (18) |
|
Projects and Using Code from Other Files |
|
|
197 | (5) |
|
|
197 | (3) |
|
Inclusions from Other Directories |
|
|
200 | (1) |
|
|
201 | (1) |
|
|
202 | (1) |
|
|
202 | (11) |
|
|
203 | (3) |
|
|
206 | (4) |
|
|
210 | (3) |
|
|
213 | (2) |
|
Chapter 8 Documentation, Error Handling, Debugging, and Testing |
|
|
215 | (32) |
|
|
215 | (7) |
|
Generating Documentation with RDoc |
|
|
216 | (2) |
|
|
218 | (1) |
|
Producing Documentation for an Entire Project |
|
|
218 | (2) |
|
|
220 | (2) |
|
|
222 | (10) |
|
Exceptions and Error Handling |
|
|
223 | (4) |
|
|
227 | (1) |
|
|
228 | (4) |
|
|
232 | (1) |
|
The Philosophy of Test-Driven Development |
|
|
233 | (6) |
|
|
236 | (2) |
|
|
238 | (1) |
|
Benchmarking and Profiling |
|
|
239 | (5) |
|
|
239 | (3) |
|
|
242 | (2) |
|
|
244 | (3) |
|
Chapter 9 Files and Databases |
|
|
247 | (50) |
|
|
247 | (22) |
|
|
248 | (1) |
|
|
249 | (20) |
|
|
269 | (8) |
|
|
269 | (3) |
|
Storing Objects and Data Structures |
|
|
272 | (5) |
|
Relational Databases and SQL |
|
|
277 | (17) |
|
Relational Database Concepts |
|
|
277 | (1) |
|
MySQL, PostgreSQL, and SQLite |
|
|
278 | (1) |
|
|
279 | (1) |
|
A Crash Course in Basic Database Operations and SQL |
|
|
280 | (5) |
|
|
285 | (6) |
|
Connecting to Other Database Systems |
|
|
291 | (2) |
|
ActiveRecord: A Sneak Peek |
|
|
293 | (1) |
|
|
294 | (3) |
|
Chapter 10 Distributing Ruby Code and Libraries |
|
|
297 | (24) |
|
Distributing Basic Ruby Programs |
|
|
297 | (4) |
|
|
299 | (1) |
|
Associated File Types in Windows |
|
|
300 | (1) |
|
Detecting Ruby's Runtime Environment |
|
|
301 | (4) |
|
Easy OS Detection with RUBY PLATFORM |
|
|
301 | (1) |
|
|
302 | (2) |
|
Accessing Command-Line Arguments |
|
|
304 | (1) |
|
Distributing Ruby Libraries As Gems |
|
|
305 | (7) |
|
|
305 | (6) |
|
|
311 | (1) |
|
|
312 | (1) |
|
Deploying Ruby Applications As Remote Services |
|
|
312 | (8) |
|
|
313 | (3) |
|
|
316 | (4) |
|
|
320 | (1) |
|
Chapter 11 Advanced Ruby Features |
|
|
321 | (22) |
|
|
321 | (7) |
|
|
322 | (2) |
|
|
324 | (2) |
|
Creating Your Own Version of attr accessor |
|
|
326 | (2) |
|
Running Other Programs from Ruby |
|
|
328 | (4) |
|
Getting Results from Other Programs |
|
|
328 | (1) |
|
Transferring Execution to Another Program |
|
|
329 | (1) |
|
Running Two Programs at the Same Time |
|
|
329 | (1) |
|
Interacting with Another Program |
|
|
330 | (2) |
|
|
332 | (3) |
|
Basic Ruby Threads in Action |
|
|
332 | (2) |
|
Advanced Thread Operations |
|
|
334 | (1) |
|
|
335 | (4) |
|
|
336 | (1) |
|
|
337 | (1) |
|
|
338 | (1) |
|
|
339 | (1) |
|
Unicode, Character Encodings, and UTF-8 Support |
|
|
339 | (3) |
|
Ruby 1.9 and Beyond's Character Encoding Support |
|
|
340 | (2) |
|
|
342 | (1) |
|
Chapter 12 Tying It Together: Developing a Larger Ruby Application |
|
|
343 | (48) |
|
|
343 | (3) |
|
|
343 | (2) |
|
|
345 | (1) |
|
|
345 | (1) |
|
Creating a Simple Text Processing Library |
|
|
346 | (14) |
|
Building the WordPlay Library |
|
|
346 | (8) |
|
|
354 | (3) |
|
|
357 | (3) |
|
|
360 | (19) |
|
The Program's Lifecycle and Parts |
|
|
361 | (1) |
|
|
362 | (5) |
|
Constructing the Bot Class and Data Loader |
|
|
367 | (2) |
|
|
369 | (6) |
|
|
375 | (4) |
|
|
379 | (4) |
|
|
379 | (4) |
|
|
383 | (1) |
|
|
383 | (6) |
|
Using Text Files As a Source of Conversation |
|
|
384 | (1) |
|
Connecting the Bot to the Web |
|
|
384 | (4) |
|
|
388 | (1) |
|
|
389 | (2) |
|
|
391 | (146) |
|
Chapter 13 Two Web Application Approaches: Rails and Sinatra |
|
|
393 | (50) |
|
|
393 | (2) |
|
The Limitations and Benefits of Our Approach |
|
|
394 | (1) |
|
Pros and Cons of the Frameworks Covered |
|
|
394 | (1) |
|
|
395 | (33) |
|
What Is Rails and Why Use It? |
|
|
395 | (3) |
|
|
398 | (2) |
|
Building a Basic Rails Application |
|
|
400 | (13) |
|
|
413 | (7) |
|
|
420 | (2) |
|
|
422 | (2) |
|
|
424 | (2) |
|
Where to Go Next: References, Books, and Example Apps |
|
|
426 | (2) |
|
Sinatra: Lightweight, Simple Web Applications |
|
|
428 | (12) |
|
The Extreme Simplicity of Sinatra |
|
|
428 | (1) |
|
General URL Routing and Parameter Matching |
|
|
429 | (2) |
|
Views, Templates, and Static Files |
|
|
431 | (5) |
|
|
436 | (4) |
|
|
440 | (3) |
|
Chapter 14 Ruby and the Internet |
|
|
443 | (28) |
|
|
443 | (15) |
|
|
444 | (10) |
|
|
454 | (4) |
|
|
458 | (4) |
|
|
458 | (2) |
|
|
460 | (2) |
|
|
462 | (6) |
|
Connection and Basic FTP Actions |
|
|
462 | (3) |
|
|
465 | (2) |
|
|
467 | (1) |
|
|
468 | (3) |
|
Chapter 15 Networking and Sockets |
|
|
471 | (22) |
|
|
471 | (2) |
|
|
471 | (1) |
|
|
472 | (1) |
|
|
473 | (4) |
|
Checking Machine and Service Availability |
|
|
473 | (1) |
|
|
474 | (3) |
|
|
477 | (13) |
|
|
477 | (3) |
|
Building a Simple TCP Server |
|
|
480 | (2) |
|
|
482 | (1) |
|
|
483 | (3) |
|
A GServer-Based Chat Server |
|
|
486 | (4) |
|
|
490 | (1) |
|
|
490 | (3) |
|
Chapter 16 Useful Ruby Libraries |
|
|
493 | (44) |
|
|
494 | (2) |
|
|
494 | (1) |
|
|
494 | (1) |
|
|
495 | (1) |
|
|
496 | (3) |
|
|
496 | (1) |
|
|
496 | (3) |
|
|
499 | (1) |
|
|
499 | (2) |
|
|
499 | (1) |
|
|
499 | (1) |
|
|
500 | (1) |
|
|
501 | (1) |
|
|
501 | (1) |
|
|
501 | (1) |
|
|
502 | (1) |
|
|
502 | (4) |
|
|
503 | (1) |
|
|
503 | (2) |
|
|
505 | (1) |
|
|
506 | (2) |
|
|
506 | (1) |
|
|
506 | (2) |
|
|
508 | (1) |
|
|
508 | (3) |
|
|
508 | (1) |
|
|
508 | (2) |
|
|
510 | (1) |
|
|
511 | (1) |
|
|
511 | (1) |
|
|
511 | (1) |
|
|
512 | (1) |
|
|
512 | (3) |
|
|
512 | (1) |
|
|
513 | (2) |
|
|
515 | (1) |
|
|
515 | (2) |
|
|
516 | (1) |
|
|
516 | (1) |
|
|
517 | (1) |
|
|
517 | (2) |
|
|
517 | (1) |
|
|
518 | (1) |
|
|
519 | (1) |
|
|
519 | (1) |
|
|
519 | (1) |
|
|
520 | (1) |
|
|
521 | (1) |
|
|
522 | (3) |
|
|
522 | (1) |
|
|
522 | (3) |
|
|
525 | (1) |
|
|
525 | (3) |
|
|
525 | (1) |
|
|
526 | (2) |
|
|
528 | (1) |
|
|
528 | (5) |
|
|
528 | (1) |
|
|
529 | (4) |
|
|
533 | (1) |
|
|
533 | (4) |
|
|
533 | (1) |
|
|
533 | (2) |
|
|
535 | (2) |
|
Appendix A Ruby Primer and Review for Developers |
|
|
537 | (34) |
|
|
537 | (4) |
|
|
537 | (3) |
|
The Ruby Interpreter and Running Ruby Code |
|
|
540 | (1) |
|
|
541 | (1) |
|
Expressions and Flow Control |
|
|
541 | (8) |
|
|
541 | (1) |
|
|
542 | (2) |
|
|
544 | (1) |
|
|
545 | (4) |
|
|
549 | (9) |
|
|
550 | (1) |
|
|
550 | (3) |
|
|
553 | (3) |
|
|
556 | (1) |
|
|
556 | (2) |
|
|
558 | (7) |
|
|
558 | (1) |
|
|
558 | (2) |
|
|
560 | (2) |
|
|
562 | (1) |
|
Hashes (Associative Arrays) |
|
|
563 | (1) |
|
|
564 | (1) |
|
|
565 | (2) |
|
|
565 | (1) |
|
|
566 | (1) |
|
|
566 | (1) |
|
|
567 | (4) |
|
|
568 | (1) |
|
|
568 | (3) |
|
Appendix B Useful Resources |
|
|
571 | (6) |
|
|
571 | (1) |
|
General Ruby Tutorials and Information |
|
|
571 | (1) |
|
|
572 | (1) |
|
|
572 | (1) |
|
|
572 | (2) |
|
|
573 | (1) |
|
|
573 | (1) |
|
|
574 | (1) |
|
|
574 | (1) |
|
|
575 | (1) |
|
|
575 | (1) |
|
|
576 | (1) |
Index |
|
577 | |