Id like to express my appreciation of all the hard work thats gone into the preparation of this book by the people at No Starch Press, especially Keith Fancher, Serena Yang, and Bill Pollock. Thanks also to the copyeditor, Kim Wimpsett, and to the technical reviewer, Pat Eyler. For keeping me on the right side of sanity, I owe a debt of gratitude to my two dogs, Beryl and Seven, and to their beautiful mother, Bethan, who, to my enormous sadness, died while I was writing this book.
Introduction
As you are now reading a book on Ruby, I think it is safe to assume you dont need me to persuade you of the merits of the Ruby language. Instead, Ill take the somewhat unconventional step of starting with a warning: Many people are attracted to Ruby by its simple syntax and its ease of use. They are wrong. Rubys syntax may look simple at first sight, but the more you get to know the language, the more you will realize that it is, on the contrary, extremely complex. The plain fact of the matter is that Ruby has a number of pitfalls just waiting for unwary programmers to drop into.
In this book, it is my aim to guide you safely over the pitfalls and lead you through the choppy waters of Rubys syntax and class libraries. In the process, Ill be exploring both the smooth, well-paved highways and the gnarlier, bumpy little byways of Ruby. By the end of the journey, you should be able to use Ruby safely and effectively without getting caught by unexpected hazards along the way.
The Book of Ruby describes versions 1.8. x and 1.9. x of the Ruby language. In most respects, Ruby 1.8 and 1.9 are very similar, and most programs written for one version will run unmodified in the other. There are important exceptions to this rule, however, and these are noted in the text. Ruby 1.9 may be regarded as a stepping stone toward Ruby 2.0. At the time of writing, a release date for Ruby 2.0 has not been announced. Even so, on the basis of currently available information, I anticipate that most (or all) of the information on Ruby 1.9 in this book should also apply to Ruby 2.0.
What Is Ruby?
Ruby is a cross-platform interpreted language that has many features in common with other scripting languages such as Perl and Python. It has an easily readable type of syntax that looks somewhat Pascal-like at first sight. It is thoroughly object-oriented and has a good deal in common with the great-granddaddy of pure object-oriented languages, Smalltalk. It has been said that the languages that most influenced the development of Ruby were Perl, Smalltalk, Eiffel, Ada, and Lisp. The Ruby language was created by Yukihiro Matsumoto (commonly known as Matz), and it was first released in 1995.
What Is Rails?
Over the past few years, much of the excitement surrounding Ruby can be attributed to a web development framework called Railspopularly known as Ruby on Rails . Rails is an impressive framework, but it is not the be-all, end-all of Ruby. Indeed, if you were to leap right into Rails development without first mastering Ruby, you might find that you end up creating applications that you dont even understand. (This is all too common among Ruby on Rails novices.) Understanding Ruby is a necessary prerequisite for understanding Rails. Youll look at Rails in .
Matters of Ruby Style
Some Ruby programmers have very fixedor even obsessiveviews on what constitutes a Ruby style of programming. Some, for example, are passionately wedded to the idea that method_names_use_underscores
while variableNamesDoNot
. The style of naming in which separate words are indicated by capital letters likeThis
is called camel case and that is the last time it will be mentioned in this book.
I have never understood why people get so worked up about naming conventions. You like underscores, I cant stand them; you say po_ta_toes, I say poTaToes. As far as I am concerned, the way in which you choose to write the names of identifiers in Ruby is of no interest to anyone but you or your programming colleagues.
That is not to say that I have no opinions on programming style. On the contrary, I have very strong opinions. In my view, good programming style has nothing to do with naming conventions and everything to do with good code structure and clarity. Language elements such as parentheses, for instance, are important. Parentheses clarify code and avoid ambiguity that, in a highly dynamic language such as Ruby, can mean the difference between a program that works as you expect and one that is full of surprises (also known as bugs ). For more on this, refer to the index entries on ambiguity and parentheses.
In more than two decades of programming, one thing I have learned through bitter experience is that the most important characteristics of well-written code are clarity and lack of ambiguity. Code that is easy to understand and easy to debug is also likely to be easier to maintain . If adopting certain naming conventions helps you achieve that goal, thats fine. If not, thats fine too. The Book of Ruby does not preach on matters of style.
How to Read This Book
The book is divided into bite-sized chunks. Each chapter introduces a theme that is subdivided into subtopics. Each programming topic is accompanied by one or more small, self-contained, ready-to-run Ruby programs.
If you want to follow a well-structured course, read each chapter in sequence. If you prefer a more hands-on approach, you can run the programs first and refer to the text when you need an explanation. If you already have some experience with Ruby, feel free to cherry-pick topics in any order you find useful. There are no monolithic applications in this book, so you dont have to worry you might lose the plot if you read the chapters out of order!
Digging Deeper
Every chapter, apart from the first one, includes a section called . This is where you will explore specific aspects of Ruby (including a few of those gnarly byways I mentioned a moment ago) in greater depth. In many cases, you could skip the Digging Deeper sections and still learn all the Ruby you will ever need. On the other hand, it is in these sections that you will often get closest to the inner workings of Ruby, so if you skip them, you are going to miss out on some pretty interesting stuff.