The Well Grounded Rubyist is a new and rewritten version of the popular Ruby for Rails. Today InfoQ publishes a review and excerpts Chapter 15.

Download: Chapter 15. Callbacks, hooks, and runtime introspection (PDF)

David A. Black’s Ruby for Rails was one of the early and popular Ruby texts. Now, Manning is publishing an extensive revision, The Well Grounded Rubyist, to be in the bookstores in March.

The Ruby language has grown in the intervening years, substantially in features and available libraries, and dramatically in acceptance and popular use. So it is certainly time for a new version. The Well Grounded Rubyist is intended to introduce the reader to Ruby, but isn’t really intended for a beginning programmer; the target audience is more the experienced programmer transitioning to Ruby. It begins with a one-chapter concise introduction to Ruby syntax including files and how classes and modules are laid out, the basics of the Ruby installation on a user system, the use of the Ruby interpreter irb , and some of the basic tools, particularly gem and rake . The first chapter (which is also available for download at the Manning site) would serve in itself to allow an experienced programmer to read and understand most Ruby code.

The book is broken into three sections. The rest of the first section, on Ruby Foundations, extends the introduction in the first chapter by covering, in separate chapters:

Objects, methods, and local variables

Organizing objects with classes

Modules and program organization

The default object (self) and scope, and visibility

Controlling program flow

The next section is on the various built in classes and modules available with Ruby, including chapters on:

Built-in essentials

Strings, symbols, numbers, and other scalar objects

Collections and container objects

Collections central: Enumerables and enumerator

Regular expressions and regex-based string operations

File, IO, and system operations

The third section is on the dynamics of Ruby. These chapters are particularly interesting because Ruby, as a dynamic language, draws much of its advantages from the powerful facilities it has for taking advantage of interpreter-time information to simplify things that can be difficult in compiled languages like C, C++, or Java. The third section contains three chapters:

Object individuation

Callable and runnable objects

Callbacks, hooks, and runtime introspection

The chapter on Callbacks, hooks, and runtime introspection is available for download directly from InfoQ, and is one of the most interesting chapters. Ruby is a descendent, in one way or another, of most of the object-oriented languages, but arguably draws the most from Smalltalk and Lisp, even though the syntax is distinctly more like a block-structured language. But the influence of Smalltalk is strongly felt in the openness of classes, allowing new code to be added at runtime to any class already available to the system, as Smalltalk does; the influence of Lisp shows in the use of code blocks and closures, and in the extensive use of continuations (“ call/cc ” in scheme, available through the yield keyword in Ruby.)

The runtime introspection and reflection facilities, allow a lot of things to be done simply in Ruby that are difficult in other languages. If an unknown method is called on a class, defining a missing_method method in the class allows that method call to be intercepted, and dealt with by user code instead of simply causing a run-time error or exception. Internals of the Class object in Ruby can be examined and extended, and the calling stack is available at run time, so user code can examine and its own state in a number of useful ways. The chapter concludes with an implementation of a very basic unit testing framework similar to the unit test frameworks provided with Ruby.

All in all, The Well-Grounded Rubyist is a strong introduction to Ruby for any experienced programmer, and will provide useful information to all but the absolutely most experienced and expert Rubyist.

Note

This excerpt is from The Well-Grounded Rubyist, to be published March 2009. It is being reproduced here by permission from Manning Publications. Manning early access books and ebooks are sold exclusively through Manning. Visit the book's page for more information.