Preface |
|
xix | |
Acknowledgments |
|
xx | |
About This Book |
|
xxii | |
About The Authors |
|
xxvii | |
About The Cover Illustration |
|
xxviii | |
Part 1: Ruby Foundations |
|
1 | (200) |
|
1 Bootstrapping your Ruby literacy |
|
|
3 | (32) |
|
1.1 Basic Ruby language literacy |
|
|
4 | (13) |
|
Installing Ruby and using a text editor |
|
|
4 | (2) |
|
A Ruby syntax survival kit |
|
|
6 | (1) |
|
The variety of Ruby identifiers |
|
|
7 | (2) |
|
Method calls, messages, and Ruby objects |
|
|
9 | (2) |
|
Writing and saving a simple program |
|
|
11 | (1) |
|
Feeding the program to Ruby |
|
|
12 | (2) |
|
|
14 | (3) |
|
1.2 Anatomy of the Ruby installation |
|
|
17 | (3) |
|
The Ruby standard library subdirectory (RbConfig::CONFIG [ "rubylibdir"]) |
|
|
18 | (1) |
|
The C extensions directory (RbConfig::CONFIG [ "archdir"] ) |
|
|
18 | (1) |
|
The site_ruby (RbConfig::CONFIG[ "sitedir"]) and vendor_ruby (RbConfig::CONFIG[ "vendordir"]) directories |
|
|
19 | (1) |
|
Standard Ruby gems and the gems directory |
|
|
19 | (1) |
|
1.3 Ruby extensions and programming libraries |
|
|
20 | (5) |
|
Loading external files and extensions |
|
|
21 | (1) |
|
"Load"-ing a file in the default load path |
|
|
22 | (1) |
|
|
23 | (1) |
|
|
24 | (1) |
|
1.4 Out-of-the-box Ruby tools and applications |
|
|
25 | (10) |
|
Interpreter command-line switches |
|
|
25 | (4) |
|
A closer look at interactive Ruby interpretation with irb |
|
|
29 | (2) |
|
The rake task-management utility |
|
|
31 | (2) |
|
Installing packages with the gem command |
|
|
33 | (2) |
|
2 Objects, methods, and local variables |
|
|
35 | (29) |
|
|
36 | (5) |
|
Ruby and object orientation |
|
|
36 | (1) |
|
Creating a generic object |
|
|
37 | (2) |
|
Methods that take arguments |
|
|
39 | (1) |
|
The return value of a method |
|
|
40 | (1) |
|
2.2 Crafting an object: the behavior of a ticket |
|
|
41 | (5) |
|
The ticket object, behavior first |
|
|
41 | (1) |
|
Querying the ticket object |
|
|
42 | (1) |
|
Shortening the ticket code via string interpolation |
|
|
43 | (1) |
|
Ticket availability: expressing Boolean state in a method |
|
|
44 | (2) |
|
2.3 The innate behaviors of an object |
|
|
46 | (4) |
|
Identifying objects uniquely with the object id method |
|
|
47 | (1) |
|
Querying an object's abilities with the respond to? method |
|
|
48 | (1) |
|
Sending messages to objects with the send method |
|
|
48 | (2) |
|
2.4 A close look at method arguments |
|
|
50 | (6) |
|
Required and optional arguments |
|
|
50 | (1) |
|
Default values for arguments |
|
|
51 | (1) |
|
Order of parameters and arguments |
|
|
52 | (2) |
|
What you can't do in argument lists |
|
|
54 | (2) |
|
2.5 Local variables and variable assignment |
|
|
56 | (8) |
|
Variables, objects, and references |
|
|
57 | (2) |
|
References in variable assignment and reassignment |
|
|
59 | (1) |
|
References and method arguments |
|
|
60 | (2) |
|
Local variables and the things that look like them |
|
|
62 | (2) |
|
3 Organizing objects with classes |
|
|
64 | (31) |
|
3.1 Classes and instances |
|
|
65 | (4) |
|
|
66 | (1) |
|
|
67 | (1) |
|
|
67 | (2) |
|
3.2 Instance variables and object state |
|
|
69 | (3) |
|
Initializing an object with state |
|
|
71 | (1) |
|
|
72 | (5) |
|
The equal sign (=) in method names |
|
|
73 | (1) |
|
Syntactic sugar for assignment-like methods |
|
|
74 | (1) |
|
|
75 | (2) |
|
3.4 Attributes and the attr_* method family |
|
|
77 | (3) |
|
Automating the creation of attributes |
|
|
78 | (2) |
|
Summary of attr_* methods |
|
|
80 | (1) |
|
3.5 Inheritance and the Ruby class hierarchy |
|
|
80 | (3) |
|
Single inheritance: one to a customer |
|
|
81 | (1) |
|
Object ancestry and the not-so-missing link: the Object class |
|
|
82 | (1) |
|
|
83 | (1) |
|
3.6 Classes as objects and message receivers |
|
|
83 | (6) |
|
|
83 | (2) |
|
How class objects call methods |
|
|
85 | (1) |
|
A singleton method by any other name... |
|
|
86 | (1) |
|
When, and why, to write a class method |
|
|
87 | (1) |
|
Class methods vs. instance methods |
|
|
88 | (1) |
|
|
89 | (3) |
|
|
90 | (1) |
|
Reassigning vs. modifying constants |
|
|
91 | (1) |
|
3.8 Nature vs. nurture in Ruby objects |
|
|
92 | (3) |
|
4 Modules and program organization |
|
|
95 | (30) |
|
4.1 Basics of module creation and use |
|
|
96 | (7) |
|
A module encapsulating "stacklikeness" |
|
|
97 | (2) |
|
Mixing a module into a class |
|
|
99 | (2) |
|
|
101 | (2) |
|
4.2 Modules, classes, and method lookup |
|
|
103 | (12) |
|
Illustrating the basics of method lookup |
|
|
103 | (3) |
|
Defining the same method more than once |
|
|
106 | (3) |
|
|
109 | (1) |
|
|
110 | (1) |
|
The rules of method lookup summarized |
|
|
111 | (1) |
|
Going up the method search path with super |
|
|
112 | (2) |
|
Inspecting method hierarchies with method and super method |
|
|
114 | (1) |
|
4.3 The method_missing method |
|
|
115 | (6) |
|
Combining method_missing and super |
|
|
116 | (5) |
|
4.4 Class/Module design and naming |
|
|
121 | (4) |
|
Mix-ins and/or inheritance |
|
|
121 | (2) |
|
Nesting modules and classes |
|
|
123 | (2) |
|
5 The default object (self), scope, and visibility |
|
|
125 | (34) |
|
5.1 Understanding self, the current/default object |
|
|
126 | (10) |
|
Who gets to be self, and where |
|
|
126 | (2) |
|
The top-level self object |
|
|
128 | (1) |
|
Self inside class, module, and method definitions |
|
|
129 | (3) |
|
Self as the default receiver of messages |
|
|
132 | (2) |
|
Resolving instance variables through self |
|
|
134 | (2) |
|
|
136 | (15) |
|
Global scope and global variables |
|
|
136 | (3) |
|
|
139 | (2) |
|
The interaction between local scope and self |
|
|
141 | (2) |
|
Scope and resolution of constants |
|
|
143 | (2) |
|
Class variable syntax, scope, and visibility |
|
|
145 | (6) |
|
5.3 Deploying method-access rules |
|
|
151 | (5) |
|
|
151 | (4) |
|
|
155 | (1) |
|
5.4 Writing and using top-level methods |
|
|
156 | (3) |
|
Defining a top-level method |
|
|
156 | (1) |
|
Predefined (built-in) top-level methods |
|
|
157 | (2) |
|
6 Control-flow techniques |
|
|
159 | (42) |
|
6.1 Conditional code execution |
|
|
160 | (12) |
|
The if keyword and its variants |
|
|
160 | (5) |
|
Assignment syntax in condition bodies and tests |
|
|
165 | (2) |
|
|
167 | (5) |
|
6.2 Repeating actions with loops |
|
|
172 | (5) |
|
Unconditional looping with the loop method |
|
|
173 | (1) |
|
Conditional looping with the while and until keywords |
|
|
174 | (2) |
|
Multiple assignment in conditional statements |
|
|
176 | (1) |
|
Looping based on a list of values |
|
|
176 | (1) |
|
6.3 Iterators and code blocks |
|
|
177 | (12) |
|
The ingredients of iteration |
|
|
177 | (1) |
|
|
178 | (1) |
|
The anatomy of a method call |
|
|
179 | (1) |
|
Curly braces vs. do/end in code block syntax |
|
|
179 | (2) |
|
|
181 | (1) |
|
The importance of being each |
|
|
182 | (2) |
|
|
184 | (1) |
|
Block parameters and variable scope |
|
|
185 | (4) |
|
6.4 Error handling and exceptions |
|
|
189 | (14) |
|
Raising and rescuing exceptions |
|
|
189 | (1) |
|
The rescue keyword to the rescue! |
|
|
190 | (2) |
|
Debugging with binding. irb |
|
|
192 | (2) |
|
Avoiding NoMethodError with the safe navigation operator |
|
|
194 | (1) |
|
Raising exceptions explicitly |
|
|
195 | (1) |
|
Capturing an exception in a rescue clause |
|
|
196 | (2) |
|
|
198 | (1) |
|
Creating your own exception classes |
|
|
198 | (3) |
Part 2: Built-In Classes And Modules |
|
201 | (212) |
|
|
203 | (30) |
|
7.1 Ruby's literal constructors |
|
|
205 | (1) |
|
7.2 Recurrent syntactic sugar |
|
|
206 | (4) |
|
Defining operators by defining methods |
|
|
207 | (2) |
|
Customizing unary operators |
|
|
209 | (1) |
|
7.3 Bang (!) methods and "danger" |
|
|
210 | (3) |
|
Destructive (receiver-changing) effects as danger |
|
|
210 | (2) |
|
Destructiveness and "danger" vary independently |
|
|
212 | (1) |
|
7.4 Built-in and custom to_* (conversion) methods |
|
|
213 | (7) |
|
String conversion: to_s and other methods defined on Object |
|
|
213 | (3) |
|
Array conversion with to_a and the * operator |
|
|
216 | (1) |
|
Numerical conversion with to_i and to_f |
|
|
217 | (2) |
|
Role-playing to_ * methods |
|
|
219 | (1) |
|
7.5 Boolean states, Boolean objects, and nil |
|
|
220 | (5) |
|
|
221 | (2) |
|
true and false as objects |
|
|
223 | (1) |
|
|
224 | (1) |
|
7.6 Comparing two objects |
|
|
225 | (4) |
|
|
226 | (1) |
|
Comparisons and the Comparable module |
|
|
227 | (2) |
|
7.7 Inspecting object capabilities |
|
|
229 | (4) |
|
Listing an object's methods |
|
|
229 | (2) |
|
Querying class and module objects |
|
|
231 | (1) |
|
Filtered and selected method lists |
|
|
231 | (2) |
|
8 Strings, symbols, and other scalar objects |
|
|
233 | (35) |
|
|
234 | (18) |
|
|
234 | (5) |
|
Basic string manipulation |
|
|
239 | (4) |
|
|
243 | (2) |
|
String comparison and ordering |
|
|
245 | (1) |
|
|
246 | (3) |
|
|
249 | (1) |
|
String encoding: a brief introduction |
|
|
250 | (2) |
|
8.2 Symbols and their uses |
|
|
252 | (6) |
|
Chief characteristics of symbols |
|
|
253 | (1) |
|
|
254 | (1) |
|
|
255 | (2) |
|
Strings and symbols in comparison |
|
|
257 | (1) |
|
|
258 | (2) |
|
|
259 | (1) |
|
Performing arithmetic operations |
|
|
259 | (1) |
|
|
260 | (8) |
|
Instantiating Date/Time Objects |
|
|
261 | (2) |
|
|
263 | (1) |
|
Date/Time Formatting Methods |
|
|
264 | (1) |
|
Date/time Conversion Methods |
|
|
265 | (3) |
|
9 Collection and container objects |
|
|
268 | (35) |
|
9.1 Arrays and hashes in comparison |
|
|
269 | (1) |
|
9.2 Collection handling with arrays |
|
|
270 | (12) |
|
|
271 | (4) |
|
Inserting, retrieving, and removing array elements |
|
|
275 | (4) |
|
Combining arrays with other arrays |
|
|
279 | (1) |
|
|
280 | (1) |
|
|
281 | (1) |
|
|
282 | (12) |
|
|
283 | (1) |
|
Inserting, retrieving, and removing hash pairs |
|
|
284 | (2) |
|
Specifying default hash values and behavior |
|
|
286 | (1) |
|
Combining hashes with other hashes |
|
|
287 | (1) |
|
|
288 | (2) |
|
|
290 | (1) |
|
Hashes as final method arguments |
|
|
291 | (1) |
|
A detour back to argument syntax: named (keyword) arguments |
|
|
292 | (2) |
|
|
294 | (3) |
|
|
294 | (1) |
|
|
295 | (2) |
|
|
297 | (6) |
|
|
298 | (1) |
|
Manipulating set elements |
|
|
298 | (3) |
|
|
301 | (2) |
|
10 Collections central: Enumerable and Enumerator |
|
|
303 | (48) |
|
10.1 Gaining enumerability through each |
|
|
305 | (2) |
|
10.2 Enumerable Boolean queries |
|
|
307 | (2) |
|
10.3 Enumerable searching and selecting |
|
|
309 | (5) |
|
Getting the first match with find |
|
|
309 | (2) |
|
Getting all matches with find_all (a.k.a. select) and reject |
|
|
311 | (1) |
|
Selecting on threequal matches with grep |
|
|
311 | (1) |
|
Organizing selection results with group_by and partition |
|
|
312 | (2) |
|
10.4 Element-wise enumerable operations |
|
|
314 | (4) |
|
|
314 | (2) |
|
The take and drop methods |
|
|
316 | (1) |
|
|
316 | (2) |
|
|
318 | (6) |
|
|
318 | (1) |
|
The each_with_index method (and each.with_index) |
|
|
318 | (1) |
|
The each_slice and each_cons methods |
|
|
319 | (1) |
|
The slice_family of methods |
|
|
320 | (1) |
|
|
321 | (1) |
|
Enumerable reduction with inject |
|
|
322 | (2) |
|
|
324 | (2) |
|
|
324 | (1) |
|
In-place mapping with map! |
|
|
325 | (1) |
|
10.7 Strings as quasi-enumerables |
|
|
326 | (2) |
|
|
328 | (4) |
|
Defining sort-order logic with a block |
|
|
330 | (1) |
|
Concise sorting with sort_by |
|
|
330 | (1) |
|
Sorting enumerables and the Comparable module |
|
|
331 | (1) |
|
10.9 Enumerators and the next dimension of enumerability |
|
|
332 | (5) |
|
Creating enumerators with a code block |
|
|
333 | (2) |
|
Attaching enumerators to other objects |
|
|
335 | (1) |
|
Implicit creation of enumerators by blockless iterator calls |
|
|
336 | (1) |
|
10.10 Enumerator semantics and uses |
|
|
337 | (6) |
|
How to use an enumerator's each method |
|
|
337 | (2) |
|
Protecting objects with enumerators |
|
|
339 | (2) |
|
Fine-grained iteration with enumerators |
|
|
341 | (1) |
|
Adding enumerability with an enumerator |
|
|
341 | (2) |
|
10.11 Enumerator method chaining |
|
|
343 | (4) |
|
Economizing on intermediate objects |
|
|
343 | (2) |
|
Indexing enumerables with with_index |
|
|
345 | (1) |
|
Exclusive-or operations on strings with enumerators |
|
|
345 | (2) |
|
|
347 | (4) |
|
FizzBuzz with a lazy enumerator |
|
|
348 | (3) |
|
11 Regular expressions and regexp-based string operations |
|
|
351 | (34) |
|
11.1 What are regular expressions? |
|
|
352 | (1) |
|
11.2 Writing regular expressions |
|
|
352 | (3) |
|
|
353 | (1) |
|
Simple matching with literal regular expressions |
|
|
353 | (2) |
|
11.3 Building a pattern in a regular expression |
|
|
355 | (3) |
|
Literal characters in patterns |
|
|
356 | (1) |
|
The dot wildcard character (.) |
|
|
356 | (1) |
|
|
357 | (1) |
|
11.4 Matching, substring Captures, and MatchData |
|
|
358 | (7) |
|
Capturing submatches with parentheses |
|
|
358 | (2) |
|
Match success and failure |
|
|
360 | (1) |
|
Two ways of getting the captures |
|
|
361 | (3) |
|
Other MatchData information |
|
|
364 | (1) |
|
11.5 Fine-tuning regular expressions with quantifiers, anchors, and modifiers |
|
|
365 | (9) |
|
Constraining matches with quantifiers |
|
|
365 | (2) |
|
Greedy (and non-greedy) quantifiers |
|
|
367 | (2) |
|
Regular expression anchors and assertions |
|
|
369 | (4) |
|
|
373 | (1) |
|
11.6 Converting strings and regular expressions to each other |
|
|
374 | (3) |
|
|
375 | (2) |
|
Going from a regular expression to a string |
|
|
377 | (1) |
|
11.7 Common methods that use regular expressions |
|
|
377 | (8) |
|
|
377 | (2) |
|
|
379 | (1) |
|
|
380 | (2) |
|
|
382 | (3) |
|
12 File and I/O operations |
|
|
385 | (28) |
|
12.1 How Ruby's I/O system is put together |
|
|
386 | (4) |
|
|
386 | (1) |
|
IO objects as enumerables |
|
|
387 | (1) |
|
|
388 | (1) |
|
A little more about keyboard input |
|
|
389 | (1) |
|
12.2 Basic file operations |
|
|
390 | (9) |
|
The basics of reading from files |
|
|
390 | (1) |
|
|
391 | (1) |
|
Byte-and character-based file reading |
|
|
392 | (1) |
|
Seeking and querying file position |
|
|
392 | (1) |
|
Reading files with File class methods |
|
|
393 | (1) |
|
|
394 | (1) |
|
Using blocks to scope file operations |
|
|
395 | (1) |
|
|
396 | (1) |
|
File I/O exceptions and errors |
|
|
397 | (2) |
|
12.3 Querying IO and File objects |
|
|
399 | (2) |
|
Getting information from the File class and the FileTest module |
|
|
399 | (2) |
|
Deriving file information with File::Stat |
|
|
401 | (1) |
|
12.4 Directory manipulation with the Dir class |
|
|
401 | (4) |
|
Reading a directory's entries |
|
|
402 | (2) |
|
Directory manipulation and querying |
|
|
404 | (1) |
|
12.5 File tools from the standard library |
|
|
405 | (10) |
|
|
406 | (2) |
|
|
408 | (1) |
|
|
409 | (2) |
|
|
411 | (2) |
Part 3: Ruby Dynamics |
|
413 | (126) |
|
|
415 | (30) |
|
13.1 Where the singleton methods are: the singleton class |
|
|
416 | (11) |
|
Dual determination through singleton classes |
|
|
417 | (1) |
|
Examining and modifying a singleton class directly |
|
|
418 | (2) |
|
Singleton classes on the method-lookup path |
|
|
420 | (5) |
|
The singleton_class method |
|
|
425 | (1) |
|
Class methods in (even more) depth |
|
|
425 | (2) |
|
13.2 Modifying Ruby's core classes and modules |
|
|
427 | (12) |
|
The risks of changing core functionality |
|
|
427 | (5) |
|
|
432 | (1) |
|
|
433 | (2) |
|
Per-object changes with extend |
|
|
435 | (3) |
|
Using refinements to affect core behavior |
|
|
438 | (1) |
|
13.3 BasicObject as ancestor and class |
|
|
439 | (6) |
|
|
439 | (2) |
|
Implementing a subclass of BasicObject |
|
|
441 | (4) |
|
14 Callable and runnable objects |
|
|
445 | (40) |
|
14.1 Basic anonymous functions: the Proc class |
|
|
446 | (10) |
|
|
446 | (1) |
|
Procs and blocks and how they differ |
|
|
447 | (1) |
|
|
448 | (4) |
|
Using Symbol#to_proc for conciseness |
|
|
452 | (1) |
|
|
453 | (3) |
|
Proc parameters and arguments |
|
|
456 | (1) |
|
14.2 Creating functions with lambda and-> |
|
|
456 | (2) |
|
|
458 | (3) |
|
|
458 | (1) |
|
The rationale for methods as objects |
|
|
459 | (2) |
|
14.4 The eval family of methods |
|
|
461 | (6) |
|
Executing arbitrary strings as code with eval |
|
|
461 | (1) |
|
|
462 | (1) |
|
|
463 | (2) |
|
Using class_eval (a.k.a. module_eval) |
|
|
465 | (2) |
|
14.5 Concurrent execution with threads |
|
|
467 | (11) |
|
Killing, stopping, and starting threads |
|
|
469 | (2) |
|
|
471 | (1) |
|
Writing a chat server using sockets and threads |
|
|
472 | (2) |
|
|
474 | (1) |
|
|
475 | (3) |
|
14.6 Issuing system commands from inside Ruby programs |
|
|
478 | (7) |
|
The system and exec methods and backticks |
|
|
479 | (2) |
|
Communicating with programs via open and popen3 |
|
|
481 | (4) |
|
15 Callbacks, hooks, and runtime introspection |
|
|
485 | (30) |
|
|
486 | (10) |
|
Intercepting unrecognized messages with method_missing |
|
|
486 | (3) |
|
Trapping include and prepend operations |
|
|
489 | (1) |
|
|
490 | (2) |
|
Intercepting inheritance with Class#inherited |
|
|
492 | (1) |
|
The Module#const_missing method |
|
|
493 | (1) |
|
The method_added and singleton_method_added methods |
|
|
494 | (2) |
|
15.2 Interpreting object capability queries |
|
|
496 | (7) |
|
Listing an object's non-private methods |
|
|
496 | (2) |
|
Listing private and protected methods |
|
|
498 | (1) |
|
Getting class and module instance methods |
|
|
499 | (2) |
|
Listing objects' singleton methods |
|
|
501 | (2) |
|
15.3 Introspection of variables and constants |
|
|
503 | (2) |
|
Listing local and global variables |
|
|
503 | (1) |
|
Listing instance variables |
|
|
504 | (1) |
|
|
505 | (4) |
|
Examining the stack trace with caller |
|
|
505 | (1) |
|
Writing a tool for parsing stack traces |
|
|
506 | (3) |
|
15.5 Callbacks and method inspection in practice |
|
|
509 | (6) |
|
Micro Test background: Mini Test |
|
|
509 | (2) |
|
Specifying and implementing Micro Test |
|
|
511 | (4) |
|
16 Ruby and functional programming |
|
|
515 | (24) |
|
16.1 Understanding pure functions |
|
|
516 | (7) |
|
Methods with side effects |
|
|
517 | (1) |
|
Pure functions and referential transparency in Ruby |
|
|
518 | (1) |
|
Side effects in Ruby's built-in methods |
|
|
519 | (2) |
|
Modifying an object's state |
|
|
521 | (2) |
|
|
523 | (3) |
|
Object#freeze and Object#frozen? |
|
|
524 | (1) |
|
|
525 | (1) |
|
16.3 Higher-order functions |
|
|
526 | (7) |
|
|
527 | (1) |
|
Kernel#itself and Kernel#yield self |
|
|
527 | (2) |
|
Functions that return functions |
|
|
529 | (1) |
|
Currying and partial function application |
|
|
529 | (4) |
|
|
533 | (6) |
|
|
534 | (2) |
|
|
536 | (3) |
Index |
|
539 | |