Preface - Programming Ruby 1.9 & 2.0: The Pragmatic Programmers’ Guide (2013)

Programming Ruby 1.9 & 2.0: The Pragmatic Programmers’ Guide (2013)

Preface

This book is a new version of the PickAxe, as Programming Ruby is known to Ruby programmers. It is a tutorial and reference for versions 1.9 and 2.0 of the Ruby programming language.

Ruby 1.9 was a significant departure from previous versions. There are major changes in string handling, the scoping of block variables, and the threading model. It has a new virtual machine. The built-in libraries have grown, adding many hundreds of new methods and almost a dozen new classes. The language now supports scores of character encodings, making Ruby one of the only programming languages to live fully in the whole world.

Ruby 2.0 is a (fairly minor) incremental improvement on Ruby 1.9.

Why Ruby?

When Andy and I wrote the first edition, we had to explain the background and appeal of Ruby. Among other things, we wrote, “When we discovered Ruby, we realized that we’d found what we’d been looking for. More than any other language with which we have worked, Ruby stays out of your way. You can concentrate on solving the problem at hand, instead of struggling with compiler and language issues. That’s how it can help you become a better programmer: by giving you the chance to spend your time creating solutions for your users, not for the compiler.”

That belief is even stronger today. More than thirteen years later, Ruby is still my language of choice: I use it for client applications and web applications. I use it to run our publishing business (our online store, http://pragprog.com , is more than 40,000 lines of Rails code), and I use it for all those little programming jobs I do just to get things running smoothly.

In all those years, Ruby has progressed nicely. A large number of methods have been added to the built-in classes and modules, and the size of the standard library (those libraries included in the Ruby distribution) has grown tremendously. The community now has a standard documentation system (RDoc), and RubyGems has become the system of choice for packaging Ruby code for distribution. We have a best-of-breed web application framework, Ruby on Rails, with others waiting in the wings. We are leading the world when it comes to testing, with tools such as RSpec and Cucumber, and we’re working through the hard problems of packaging and dependency management. We’ve matured nicely.

But Ruby is older than that. The first release of this book happened on Ruby’s 20th birthday (it was created on February 24, 1993). The release of Ruby 2.0 is a celebration of that anniversary.

Ruby Versions

This version of the PickAxe documents both Ruby 2.0 and Ruby 1.9.3.[1]

Exactly what version of Ruby did I use to write this book? Let’s ask Ruby:

$ ruby -v

ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-darwin12.2.0]

This illustrates an important point. Most of the code samples you see in this book are actually executed each time I format the book. When you see some output from a program, that output was produced by running the code and inserting the results into the book.

Changes in the Book

Throughout the book I’ve tried to mark differences between Ruby 1.9 and 2.0 using a small symbol, like the one here2.0». If you’re reading this as an ebook, you’ll see little arrows next to this flag. Clicking those will take you to the next or previous 2.0 change. One change I didn’t make: I decided to continue to use the word we when talking about the authors in the body of the book. Many of the words come from the first edition, and I certainly don’t want to claim any credit for Andy’s work on that book.

Changes in the Ruby 2.0 Printing

Compared to the major change that occurred between Ruby 1.8 and Ruby 1.9, the update to Ruby 2 is fairly gentle. This book documents all the updated builtin class changes and the new keyword arguments. It spends some time looking at lazy enumerators, and at the updates to the regular expression engine. But, in general, users of Ruby 1.9 will feel right at home, and folks still using Ruby 1.8 should consider skipping straight to Ruby 2.

Notation Conventions

Literal code examples are shown using a sans-serif font:

class SampleCode

def run

#...

end

end

Within the text, Fred#do_something is a reference to an instance method (in this case the method do_something ) of class Fred, Fred.new[4] is a class method, and Fred::EOF is a class constant. The decision to use a hash character to indicate instance methods was a tough one. It isn’t valid Ruby syntax, but we thought that it was important to differentiate between the instance and class methods of a particular class. When you see us write File.read, you know we’re talking about the class method read . When instead we write File#read, we’re referring to the instance method read . This convention is now standard in most Ruby discussions and documentation.

This book contains many snippets of Ruby code. Where possible, we’ve tried to show what happens when they run. In simple cases, we show the value of expressions on the same line as the expression. Here’s an example:

a = 1

b = 2

a + b # => 3

Here, you can see that the result of evaluating a + b is the value 3, shown to the right of the arrow. Note that if you were to run this program, you wouldn’t see the value 3 output—you’d need to use a method such as puts to write it out.

At times, we’re also interested in the values of assignment statements:

a = 1 # => 1

a + 2 # => 3

If the program produces more complex output, we show it after the program code:

3.times { puts "Hello!" }

Produces:

Hello!

Hello!

Hello!

In some of the library documentation, we wanted to show where spaces appear in the output. You’ll see these spaces as ␣ characters.

Command-line invocations are shown with literal text in a regular font, and parameters you supply are shown in an italic font. Optional elements are shown in brackets.

ruby <flags>* progname <arguments>*

Footnotes

[1]

Ruby version numbering used to follow the same scheme used for many other open source projects. Releases with even minor version numbers—1.6, 1.8, and so on—were stable, public releases. These are the releases that are prepackaged and made available on the various Ruby websites. Development versions of the software had odd minor version numbers, such as 1.5 and 1.7. However, in 2007 Matz broke with convention and made 1.9 a stable public release of Ruby.

[2]

http://www.pragprog.com/titles/ruby4/errata.html

[3]

http://www.flickr.com/photos/pragdave/sets/72157625046498937/

[4]

In some other Ruby documentation, you may see class methods written as Fred::new. This is perfectly valid Ruby syntax; we just happen to think that Fred.new is less distracting to read.