Eloquent Ruby (US | UK) by Russ Olsen is a book about the Ruby programming language that focuses on Ruby programming style by teaching you how to write your code as a real Rubyist.
This is the second Olsen’s book about Ruby. In 2007 he published Design Patterns in Ruby, an other great example of how common programming practices like Design Patterns apply to the Ruby world.
Eloquent Ruby is a very lightweight and pleasant reading. The colloquial tone is friendly and engrossing. The books has plenty of code snippets and it requires only a few days to read it from start to end.
Be prepared, this is an unconventional Ruby book. To use author’s words
This is a book about making that final leap, about absorbing the Ruby programming culture, about becoming truly fluent in Ruby.
This is something I have been claiming for the last 5 years. Learning Ruby is not difficult, start thinking in Ruby and becoming a Rubyist is the real challenge.
A formal understanding of the mechanics of Ruby isn’t the same as really looking at the programming world through Ruby-colored glasses. You need to absorb the cultural part of Ruby, to see how real Rubyist use the language to solve problems.
This is one of the reasons that convinced me to create the Rubyist.
Structure
The book is divided into 4 parts:
- The Basics
- Classes, Modules, and Blocks
- Metaprogramming
- Pulling It All Together
Each part is divided into chapters. The book counts 31 chapters and about 400 pages.
The first part covers some basic Ruby features in a way you normally won’t read in any other Ruby reference. Ah, if I only had the Symbol chapter available when I started learning Ruby!
The second part covers Classes, Modules and Blocks and it explains how to use modules successfully, how to deal with inheritance, equality and operators. This is by far my most favorite section.
The third part is about Metaprogramming. Whilst the name of the section is technically correct, it can be misleading. If you are looking for a complete Ruby and Metaprogramming course, check out Metaprogramming Ruby (US | UK). This section covers common Ruby metaprogramming topic such as hooks, method_missing (a must read!) and monkey patching.
The forth part wraps several topics all together and talks about creating and implementing a DSL in Ruby.
The book ends with a rich list of books about Ruby and programming in general. The list contains amazing Ruby titles like Ruby Best Practices (US | UK) or The Ruby Way Second Edition (US | UK), as well programming masterpieces like The Elements of Programming Style (US | UK). If you are looking for some inspiration about your next reading, you might probably find some there.
Chapters
- Write Code That Looks Like Ruby
- Choose the Right Control Structure
- Take Advantage of Ruby’s Smart Collections
- Take Advantage of Ruby’s Smart Strings
- Find The Right String With Regular Expressions
- Use Symbols to Stand for Something
- Treat Everything Like an Object – Because Everything Is
- Embrace Dynamic Typing
- Write Specs!
- Construct Your Classes from Short, Focused Methods
- Define Operators Respectfully
- Create Classes That Understand Equality
- Get The Behavior You Need with Singleton and Class Methods
- Use Class Instance Variables
- Use Modules as Name spaces
- Use Modules as Mixins
- Use Blocks to Iterate
- Execute Around with a Block
- Save Blocks to Execute Later
- Use Hooks to Keep Your Program Informed
- Use method_missing for Flexible Error Handling
- Use method_missing for Delegation
- Use method_missing to Build Flexible APIs
- Update Existing Classes with Monkey Patching
- Create Self-Modifying Classes
- Create Classes That Modify Their Subclasses
- Invent Internal DSLs
- Build External DSLs for Flexible Syntax
- Package Your Programs as Gems
- Know Your Ruby Implementation
- Keep An Open Mind to Go With Those Open Classes
Requirements
This book assumes that you have a basic knowledge of the Ruby language. You don’t need to be a Ruby master, but some advanced sections such as Metaprogramming and DSL may require you to stop for a moment and refresh or improve your specific knowledge of Ruby on that topic.
Don’t expect this book to explain you the basic details of Ruby or its syntax, this is behind the scope of this publication. There are plenty of commented examples, but if you want to learn about a specific Ruby feature make sure you keep a reference like Programming Ruby (US | UK) or The Ruby Way (US | UK) handy.
Another requirement is that you must be ready to read printed source code: this book is full or Ruby code. At least the 50% of the pages contain code, making this book a valuable practical reference.
Plus
Aside from being an excellent resource to help you thinking Ruby and programming in the Ruby way, this book constantly adopts a practical approach providing tons of examples to read. Every chapter ends with an In the Wild section containing examples extracted from real Ruby libraries, and a Wrapping up section that helps you to fix the concepts in mind.
As as long time Ruby developer I really loved the Use Symbols to Stand for Something, object and block chapters. Some of them didn’t necessary teach anything new, but the way used to present the concepts was very helpful and understandable.
I appreciate the focus on tests and the RSpec chapter. The most part of Ruby books dedicate a specific section about tests, then seem to forget them for the rest of the chapters. This is not the case of this book, where several examples are verified by tests.
Finally, the simple colloquial tone makes the book really friendly. It seems like a person you always worked with is now teaching you programming and thinking in the Ruby way.
Minus
It’s really hard to find some real negative in this book.
I found the Regular Expression chapter pretty boring and misplaced. In fact, it was the only chapter in the book where the main focus was teaching Regular Expression basics, instead of focusing on using Regular Expressions in the right way.
The same applies to the RubyGems sections. There have been several changes in the Gems community in the last years and the chapter appears to be slightly outdated. Again, I would have left this topic outside the book.
Finally, I would have appreciated a wider usage of Ruby 1.9 over 1.8 in order to discourage the adoption of Ruby 1.8.
In Conclusion
If you are a beginner to intermediate level Ruby programmer, this book is a must read that it’s likely to help you improving your Ruby skill and writing code in the Ruby way.
If you are completely new to Ruby, personally don’t recommend this book as a first reading. You might want to start with a Ruby reference to learn how programming in Ruby, then read Eloquent Ruby to learn how programming in the Ruby way. Also, don’t forget about the Rubyist, you’ll find several Ruby common practices and coding conventions.
If you are a Ruby expert and you have been writing Ruby for the last 5 years, don’t be too self-confident. I’m quite sure the book will be able to provide you some valuable advice more than once.
Where to Find Eloquent Ruby
You can get a printed or Kindle copy at Amazon.com, Amazon.co.uk, or at your other favorite book seller.