About this Book - The Well-Grounded Rubyist, Second Edition (2014)

The Well-Grounded Rubyist, Second Edition (2014)

About this Book

Welcome

... to the second edition of The Well-Grounded Rubyist.

Ruby is a general-purpose, object-oriented, interpreted programming language designed by Yukihiro “Matz” Matsumoto. Ruby was first announced in 1993. The first public release appeared in 1995, and the language became very popular in Japan during the 1990s. It’s known and admired for its expressiveness—its ability to do a lot with relatively little code—and the elegance and visual smoothness of its syntax and style. Ruby has proven useful and productive in a wide variety of programming contexts, ranging from administrative scripting to device embedding, from web development to PDF document processing. Moreover, and at the risk of sounding non-technical, Ruby programming is fun. It’s designed that way. As Matz has said, Ruby is optimized for the programmer experience. Indeed, Ruby started as Matz’s pet project and gained attention and traction because so many other programmers got pleasure from the same kind of language design that Matz did.

The first English-language book on Ruby (Programming Ruby by Dave Thomas and Andy Hunt [Addison-Wesley]) appeared in late 2000 and ushered in a wave of Ruby enthusiasm outside of Japan. Ruby’s popularity in the West has grown steadily since the appearance of the “Pickaxe book” (the nickname of the Thomas-Hunt work, derived from its cover illustration). Four years after the first edition of the Pickaxe, the introduction of the Ruby on Rails web application development framework by David Heinemeier Hansson sparked a massive surge in worldwide interest in Ruby. The years since 2004 have seen exponential growth in the use of Ruby, as well as books about Ruby, Ruby user groups, and Ruby-related conferences and other events.

I’m a Rails developer and devotee. At the same time, I’m firmly convinced that even if Rails had never come along, the world would have “discovered” Ruby eventually on the scale that we’re seeing in the Rails era. Ruby is too pleasing and versatile a language to have remained a semi-secret jewel forever. I’ve loved Ruby for almost 14 years, and it has been my pleasure to introduce a large number of people to the language through my writing and teaching, and to watch the vast majority of those people embrace Ruby with pleasure and satisfaction.

And that’s why I wrote this book. The purpose of The Well-Grounded Rubyist is to give you a broad and deep understanding of how Ruby works and a considerable toolkit of Ruby techniques and idioms that you can use for real programming.

How this book is organized

The Well-Grounded Rubyist, Second Edition consists of 15 chapters and is divided into 3 parts:

· Part 1: Ruby foundations

· Part 2: Built-in classes and modules

· Part 3: Ruby dynamics

Part 1 (chapters 1 through 6) introduces you to the syntax of Ruby and to a number of the key concepts and semantics on which Ruby programming builds: objects, methods, classes and modules, identifiers, and more. It also covers the Ruby programming lifecycle (how to prepare and execute code files, writing programs that span more than one file), as well as many of the command-line tools that ship with Ruby and that Ruby programmers use frequently, including the interactive Ruby interpreter (irb), the RubyGems package manager (gem), and the Ruby interpreter (ruby).

Part 2 (chapters 7 through 12) surveys the major built-in classes—including strings, arrays, hashes, numerics, ranges, dates and times, and regular expressions—and provides you with insight into what the various built-ins are for, as well as the nuts and bolts of how to use them. It also builds on your general Ruby literacy with exploration of such topics as Boolean logic in Ruby, built-in methods for converting objects from one class to another (for example, converting a string to an integer), Ruby’s considerable facilities for engineering collections and their enumeration, and techniques for comparing objects for identity and equality. You’ll also learn about file and console I/O as well as issuing system commands from inside Ruby programs.

Part 3 (chapters 13 through 15) addresses the area of Ruby dynamics. Under this heading you’ll find a number of subtopics—among them some metaprogramming techniques—including Ruby’s facilities for runtime reflection and object introspection; ways to endow objects with individualized behaviors; and the handling of functions, threads, and other runnable and executable objects. This part of the book also introduces you to techniques for issuing system commands from inside a Ruby program and encompasses a number of Ruby’s event-triggered runtime hooks and callbacks, such as handlers for calls to non-existent methods and interception of events like class inheritance and method definition.

Ruby is a system, and presenting any system in a strictly linear way is a challenge. I meet the challenge by thinking of the learning process as a kind of widening spiral, building on the familiar but always opening out into the unknown. At times, you’ll be shown enough of a future topic to serve as a placeholder, so that you can learn the current topic in depth. Later, with the necessary bootstrapping already done, you’ll come back to the placeholder topic and study it in its own right. The Well-Grounded Rubyist, Second Edition is engineered to expose you to as much material as possible as efficiently as possible, consistent with its mission of providing you with a solid foundation in Ruby—a real and lasting understanding of how the language works.

Who should read this book

The Well-Grounded Rubyist, Second Edition is optimized for a reader who’s done some programming and perhaps even some Ruby and wants to learn more about the Ruby language—not only the specific techniques (although the book includes plenty of those), but also the design principles that make Ruby what it is. I’m a great believer in knowing what you’re doing. I also believe that knowing what you’re doing doesn’t mean you have to compose a treatise in your head every time you write a line of code; it means you know how to make the most out of the language, and understand how to analyze problems when they arise.

I’ve hedged my bets a little, in terms of targeted readership, in that I’ve included some introductory remarks about a number of topics and techniques that are possibly familiar to experienced programmers. I ask the indulgence of those readers. The remarks in question go by pretty quickly, and I believe that even a few words of explanation of terms here and there can make a surprisingly big difference in how many people feel at home in, and welcomed by, the book. If you’re a more experienced programmer and see passages where I seem to be spoon-feeding, please bear with me. It’s for a good cause.

By the same token, if this is your first foray into programming, be prepared to do a little extra self-imposed “homework” to get ramped up into the programming process—but by all means, give The Well-Grounded Rubyist, Second Edition a go. The book isn’t specifically an introduction to programming, but it does take you through all the practicalities, including the creation and running of program files, as well as explaining Ruby from the ground up.

What this book doesn’t include

The Well-Grounded Rubyist, Second Edition is a serious, extensive look at the Ruby language. But it isn’t a complete language reference. There are core classes that I say little or nothing about, and I discuss only a modest number of standard library packages. That’s by design. You don’t need me to spell out for you how to use every standard-library API, and I don’t. What you do need, in all likelihood, is someone to explain to you exactly what class << self means, or why two instance variables two lines apart aren’t the same variable, or the distinction between singleton methods and private methods, or what an enumerator is and how it differs from an iterator. You need to know these things, and you need to see them in operation and to start using them. You must, of course, plunge deeply into the standard library in your work with Ruby, and I’m not encouraging you not to. I’m aiming to impart a particular kind and degree of understanding in this book.

A word on Ruby versions

The Well-Grounded Rubyist, Second Edition covers version 2.1 of the Ruby language, the most recent version at time of writing. Version 1.9 is still in wide use, though I predict it won’t be for much longer. If you’re still using 1.9, you’ll get a lot of value from the book—especially as the odds are that you’ll make the transition to some flavor of Ruby 2 in the not-too-distant future.

Code conventions, examples, and downloads

In the text, names of Ruby variables and constants are in monospace. Names of classes and modules are in monospace where they represent direct references to existing class or module objects; for example, “Next, we’ll reopen the class definition block for Person.” Where the name of a class or module is used in a more high-level narrative sense, the name appears in regular type; for example, “Now we need an Array instance.” In all cases, you’ll be able to tell from the context that a class, module, or other Ruby entity is under discussion.

Source code for all of the working examples in this book is available for download from www.manning.com/black3 or www.manning.com/TheWellGroundedRubyist-SecondEdition.

Names of programs, such as ruby and rails, are in monospace font where reference is made directly to the program executable or to command-line usage; otherwise, they appear in regular type.

Italics or an asterisk are used for wildcard expressions; for example, to_* might indicate the general category of Ruby methods that includes to_i and to_s, whereas position_match might correspond to post_match or pre_match.

You can run the standalone code samples in the book either by placing them in a text file and running the ruby command on them, or by typing them into the Interactive Ruby interpreter irb. In chapter 1, you’ll learn these techniques. As the book progresses, it will be assumed that you can do this on your own and that you’ll make up names for your sample files if no names are suggested (or if you prefer different names).

A considerable number of examples in the book are presented in the form of irb sessions. What you’ll see on the page are cut-and-pasted lines from a live interactive session, where the code was entered into irb and irb responded by running the code. You’ll come to recognize this format easily (especially if you start using irb yourself). This mode of presentation is particularly suitable for short code snippets and expressions; and because irb always prints out the results of executing whatever you type in (rather like a calculator), it lets you see results while economizing on explicit print commands.

In other cases, the output from code samples is printed separately after the samples, printed alongside the code (and clearly labeled as output), or embedded in the discussion following the appearance of the code.

Some examples are accompanied by numbered cueballs that appear to the side of the code. These cueballs are linked to specific points in the ensuing discussion and give you a way to refer back quickly to the line under discussion.

Command-line program invocations are shown with a dollar-sign ($) prompt, in the general style of shell prompts in UNIX-like environments. Most of these commands will work on Windows, even though the prompt may be different. (In all environments, the availability of the commands depends, as always, on the setting of the relevant path environment variable.)

The use of web rather than Web to designate the World Wide Web is a Manning in-house style convention that I have followed here, although in other contexts I follow the W3C’s guideline, which is to use Web.