Read an Excerpt
You can find some great Ruby books on the market. If you are new to Ruby, a friend or someone on the Internet has probably already listed some favorite Ruby booksand you should buy those books. But every book has its niche: Each attempts to appeal to a certain need of a programmer.
It is my belief that the best thing this book can do for you is show you the code. I promise to keep the chat to a minimum, to focus instead on the quality and quantity of actual Ruby code. I'll also keep as much useful information in as tight a space as is possible.
Unlike any other book on the market at the time of this writing, this book is intended to be a (laptop-bag) "pocket-size" resource that enables you to quickly look up a topic and find examples of practical Ruby codea topical quick reference, if you will. In each of the topics covered, I try to provide as thorough an approach to each task as the size allows for; there's not as much room for coverage of topical solutions as there is in much larger books with similar goals, such as The Ruby Way, 2nd Edition (Sams, 2006), by Hal Fulton. Because of this, other issues that are often given equal priority are relegated to second. For instance, this is not a tutorial; the code samples have some explanation, but I assume that you have a passing familiarity with the language. Also, when possible, I try to point out issues related to security and performance, but I make no claim that these are my highest priority.
I hope that you find this book a useful tool that you keep next to your keyboard whenever you are phrase-mongering inRuby.How to Use This Book
I have not intended for this book to be read cover to cover. Instead, you should place your bookmark at the Table of Contents so you can open the book, find the topic you are programming on at the moment, and go immediately to a description of all the issues you might run into.
The content in the book is arranged by topic instead of Ruby class. The benefit is that you can go to one place in this book instead of four or five areas in Ruby's own documentation. (Not that there's anything wrong with Ruby's documentation. It's just that sometimes you are working with several classes at a time and, because Ruby's docs are arranged by class, you have to jump around a lot.)Conventions
Phrases throughout the book are put in dark gray boxes at the beginning of every topic.
Phrases look like this.
Code snippets that appear in normal text are in italics. All other code blocks, samples, and output appear as follows:
# code sample boxes.
Parentheses are optional in Ruby in some casesthe rule is: you must have parentheses in your method call if you are calling another function in your list of parameters, or passing a literal code block to the method. In all other cases, parentheses are optional. Personally, I'm a sucker for consistency but one of the indisputable strengths of Ruby is the flexibility of the syntax.
In an attempt to have consistency between this book and others, I will (reluctantly) use .class_method() to refer to class methods, ::class_variable to refer to class variables, #method() to refer to instance methods, and finally #var to refer to instance variables. When referring to variables and methods which are members of the same class, I'll use the appropriate @variable and @@class_varriable.
I know that some people might find these two rules annoyingespecially those coming from languages that use the '::' and '.' notation everywhere. In all practicality, you will never be so consistentand rightfully so. One of Ruby's strengths is that there is a ton of flexibility. In fact, this flexibility has helped make Ruby on Rails so popular. This allowed the creators of Rails to make what appears to be a domain-specific language (a language well-suited for a specific kind of work) for web development. But really, all that is going on is a variation on Ruby syntax. And this is one of the many reasons that Ruby is more suitable for a given problem than, say, Python. Python's rigidity ("there should be oneand preferably only oneobvious way to do it") doesn't lend itself to DSL, so the programmers in that language are forced to use other means (which might or might not turn out to be unpleasant).
I always use single quotes (') in Ruby code unless I actually want to make use of the double-quote (")features (interpolation and substitution).
I always put the result of the evaluation of the statement (or block) on the next line with a proceeding #=>, similar to what you would find if you were using irb or browsing Ruby's documentation.
Comments on executable lines of code start with # and are in italics to the end of the comment. Comments on #=> lines are in parentheses and are in italics.Acknowledgments
Without the Pragmatic Programmers' freely available 1st Edition of Programming Ruby, I would have never discovered the wonderful world of Ruby. The Pickaxe books and the great Ruby community are what make projects like this one possible.
Thanks to my loving partner, Brandon S. Ward, for his infinite patience while working on this book.Reporting Errata
Readers will almost certainly find topics that they wish were covered which we were overlooked when planning this book. I encourage you to please contact us and let us know what you would like to see included in later editions. Criticisms are also welcome. Contact information can be found in the front-matter of this book.
© Copyright Pearson Education. All rights reserved.