Life Is Short
This is a book of recipes: solutions to common problems, copy-and-paste code snippets, explanations, examples, and short tutorials.
This book is meant to save you time. Time, as they say, is money, but a span of time is also a piece of your life. Our lives are better spent creating new things than fighting our own errors, or trying to solve problems that have already been solved. We present this book in the hope that the time it saves, distributed across all its readers, will greatly outweigh the time we spent creating it.
The Ruby programming language is itself a wonderful time-saving tool. It makes you more productive than other programming languages because you spend more time making the computer do what you want, and less wrestling with the language. But there are many ways for a Ruby programmer to spend time without accomplishing anything, and weve encountered them all:
Time spent writing Ruby implementations of common algorithms.
Time spent debugging Ruby implementations of common algorithms.
Time spent discovering and working around Ruby-specific pitfalls.
Time spent on repetitive tasks (including repetitive programming tasks!) that could be automated.
Time spent duplicating work that someone else has already made publicly available .
Time spent searching for a library that does x.
Time spent evaluating and deciding between the many libraries that do x.
Time spent learning how to use a library because of poor or outdated documentation .
Time lost staying away from a useful technology because it seems intimidating.
We, and the many contributors to this book, recall vividly our own wasted hours and days. Weve distilled our experiences into this book so that you dont waste your timeor at least so you waste it enjoyably on more interesting problems.
Our other goal is to expand your interests. If you come to this book wanting to generate algorithmic music with Ruby then, yes, will save you time over starting from scratch. Its more likely that youd never considered the possibility until now. Every recipe in this book was developed and written with these two goals in mind: to save you time, and to keep your brain active with new ideas.
Audience
This cookbook is aimed at people who know at least a little bit of Ruby, or who know a fair amount about programming in general. This isnt a Ruby tutorial (see below for some real tutorials), but if youre already familiar with a few other programming languages, you should be able to pick up Ruby by reading through the first 10 chapters of this book and typing in the code listings as you go.
Weve included recipes suitable for all skill levels, from those who are just starting out with Ruby, to experts who need an occasional reference. We focus mainly on generic programming techniques, but we also cover specific application frameworks (like Ruby on Rails and GUI libraries) and best practices (like unit testing).
Even if you just plan to use this book as a reference, we recommend that you skim through it once to get a picture of the problems we solve. This is a big book, but it doesnt solve every problem. If you pick it up and you cant find a solution to your problem, or one that nudges you in the right direction, then youve lost time.
If you skim through this book once beforehand, youll get a fair idea of the problems we cover in this book, and youll get a better hit rate. Youll know when this book can help you, and when you should consult other books, do a web search, ask a friend, or get help some other way.
The Structure of This Book
Each of this books chapters focuses on a kind of programming or a particular data type. This overview of the chapters should give you a picture of how we divided up the recipes. Each chapter also has its own, somewhat lengthier introduction, which gives a more detailed view of its recipes. At the very least, we recommend you skim the chapter introductions and the table of contents.
A brand new chapter covers what has changed since Ruby 1.8 when the first version of this book was released:
The next six chapters cover Rubys built-in data structures:
by Jeffrey Friedl (OReilly).
).
, covers Rubys two interfaces for dealing with time: the one based on the C time library, which may be familiar to you from other programming languages, and the one implemented in pure Ruby, which is more idiomatic.
.
, covers the hash, Rubys other basic compound data type. Hashes make it easy to associate objects with names and find them later (hashes are sometimes called lookup tables or dictionaries, two telling names). Its easy to use hashes along with arrays to build deep and complex data structures.
.
The first six chapters deal with specific algorithmic problems. The next four are more abstract: theyre about Ruby idiom and philosophy. If you cant get the Ruby language itself to do what you want, or youre having trouble writing Ruby code that looks the way Ruby should look, the recipes in these chapters may help: