In this chapter, we’ll explore Ruby’s facilities for pattern matching and text processing, centering around the use of regular expressions. A regular expression in Ruby serves the same purposes it does in other languages: it specifies a pattern of characters, a pattern that may or may not correctly predict (that is, match) a given string.
Pattern-match operations are used for conditional branching (match/no match), pinpointing substrings (parts of a string that match parts of the pattern), and various text-filtering techniques.
Regular expressions in Ruby are objects. You send messages to a regular expression.
Regular expressions add something to the Ruby landscape but, as objects, they also fit nicely into the landscape.
We’ll start with an overview of regular expressions. From there, we’ll move on to the details of how to write them and, of course, how to use them. In the latter category, we’ll look at using regular expressions both in simple match operations and in methods where they play a role in a larger process, such as filtering a collection or repeatedly scanning a string.
1 ■ Bootstrapping your Ruby literacy 3
2 ■ Objects, methods, and local variables 34
3 ■ Organizing objects with classes 62
4 ■ Modules and program organization 92
5 ■ The default object (self), scope, and visibility 119
6 ■ Control-flow techniques 152
PART 2BUILT-IN CLASSES AND MODULES ………………………….189
7 ■ Built-in essentials 191
8 ■ Strings, symbols, and other scalar objects 219
9 ■ Collection and container objects 254
10 ■ Collections central: Enumerable and Enumerator 286
11 ■ Regular expressions and regexp-based
string operations 330
12 ■ File and I/O operations 360 BRIEF CONTENTS
PART 3RUBY DYNAMICS ………………………………………………..387
13 ■ Object individuation 389
14 ■ Callable and runnable objects 418
15 ■ Callbacks, hooks, and runtime introspection 456