As its title implies, this book covers the Ruby programming language and aspires to do so comprehensively and accessibly. This edition of the book covers language versions 1.8 and 1.9. Ruby blurs the distinction between language and platform, and so our coverage of the language includes a detailed overview of the core Ruby API. But this book is not an API reference and does not cover the core classes comprehensively. Also, this is not a book about Ruby frameworks (like Rails), nor a book about Ruby tools (like rake and gem).
This chapter concludes with a heavily commented extended example demonstrating a nontrivial Ruby program. The chapters that follow cover Ruby from the bottom up:
Chapter 2 covers the lexical and syntactic structure of Ruby, including basic issues like character set, case sensitivity, and reserved words.
Chapter 3 explains the kinds of data—numbers, strings, ranges, arrays, and so on—that Ruby programs can manipulate, and it covers the basic features of all Ruby objects.
Chapter 4 covers primary expressions in Ruby—literals, variable references, method invocations, and assignments—and it explains the operators used to combine primary expressions into compound expressions.
Chapter 5 explains conditionals, loops (including blocks and iterator methods), exceptions, and the other Ruby expressions that would be called statements or control structures in other languages.
Chapter 6 formally documents Ruby’s method definition and invocation syntax, and it also covers the invocable objects known as procs and lambdas. This chapter includes an explanation of closures and an exploration of functional programming techniques in Ruby.
Chapter 7 explains how to define classes and modules in Ruby. Classes are fundamental to object-oriented programming, and this chapter also covers topics such as inheritance, method visibility, mixin modules, and the method name resolution algorithm.
Chapter 8 covers Ruby’s APIs that allow a program to inspect and manipulate itself, and then demonstrates metaprogramming techniques that use those APIs to make programming easier. The chapter includes an example of domain-specific language.
Chapter 9 demonstrates the most important classes and methods of the core Ruby platform with simple code fragments. This is not a reference but a detailed overview of the core classes. Topics include text processing, numeric computation, collections (such as arrays and hashes), input/output, networking, and threads. After reading this chapter, you’ll understand the breadth of the Ruby platform, and you’ll be able to use the ri tool or an online reference to explore the platform in depth.
Chapter 10 covers the top-level Ruby programming environment, including global variables and global functions, command-line arguments supported by the Ruby interpreter, and Ruby’s security mechanism.
It is easy to program in Ruby, but Ruby is not a simple language. Because this book documents Ruby comprehensively, it is not a simple book (though we hope that you find it easy to read and understand). It is intended for experienced programmers who want to master Ruby and are willing to read carefully and thoughtfully to achieve that goal.
Like all similar programming books, this book contains forward and backward references throughout. Programming languages are not linear systems, and it is impossible to document them linearly. As you can see from the chapter outline, this book takes a bottom-up approach to Ruby: it starts with the simplest elements of Ruby’s grammar and moves on to document successively higher-level syntactic structures—from tokens to values to expressions and control structures to methods and classes. This is a classic approach to documenting programming languages, but it does not avoid the problem of forward references.
The book is intended to be read in the order it is written, but some advanced topics are best skimmed or skipped on the first reading; they will make much more sense when you come back to them after having read the chapters that follow. On the other hand, don’t let every forward reference scare you off. Many of them are simply informative, letting you know that more details will be presented later. The reference does not necessarily imply that those future details are required to understand the current material.