Book review - Eloquent Ruby by Russ Olsen
Sun, Mar 6, 2011I recently spent a couple of evenings reading this book and found it to be an easy to read, informative book about ruby best practices that I would highly recommend to intermediate ruby developers who want to confirm that they are going in the right direction.
I’ve been programming in Ruby for about 3 or 4 years now, and I think of myself somewhere a little beyond intermediate, but I would not call myself an expert rubyist by any means. I think this book is probably aimed at (early) intermediate Ruby developers with maybe 1 or 2 years experience. So for me it was more of a confirmation of the things I have learned over the last few years from other developers, other books and the various ruby blogs and websites.
This is not a ‘how to program in ruby’ book. Beginners should go out and get the pickaxe or The Ruby Way.
Neither is this an advanced metaprogramming book. While it does have some very nice explanations for the basics of metaprogramming it should only really be considered an introduction to the ideas, for more depth go and look at Metaprogramming Ruby.
This is the kind of book that helps confirm things like:
- object equality - Whats the difference between ==, ===, equal? and eql?
- Why are class variables bad ? Why are class instance variables better ?
- What is the difference between include vs extend ?
- What is the singleton class (eigenclass) ?
My Thoughts
The book itself is very easy to read. It has a very comfortable writing style and some complex ideas are presented in a surprisingly easy to understand way. The chapters are short and bite-sized, which not only makes them painless, but also tempts you to read ‘just one more chapter’… before you know it you are done!
The book is organized well, broken up into 4 parts:
- The Basics
- Classes, Modules and Blocks
- Metaprogramming
- Pulling it all Together.
Part I is about helping you write code that looks like ruby and follow styles used by the ruby community, e.g. using Enumerable methods instead of for loops, using case statements correctly, symbols vs strings etc.
Part 2 covers best practices for creating classes and using modules and blocks, e.g. using short focused methods, understanding object equality, using modules as namespaces and mixins, using blocks to iterate etc.
Part 3 introduces the basics of Metaprogramming, e.g. method_missing, open classes and monkey patching, defining methods etc.
Part 4 is really about using ruby to build DSL’s.
Most of this was already familiar to me, but it is still nice to see it presented in a clear and concise way. I found Part 2 to be the most useful, reinforcing some of the fundamental pieces of building Ruby classes.
- The composed method where each method does a single thing, operates at a single conceptual level, and has a meaningful human friendly name.
- Getting object equality right.
- The singleton class (eigenclass) that sits between every object and its class.
- How class methods are just singleton methods on a class.
- Why @@ class variables are considered bad, and @ class instance variables are the better approach.
- Fundamental difference between include vs extend, and the included hook
Part 3 was a good revision of metaprogramming basics. Again, I am already comfortable with a lot of these techniques, but it never hurts to go back and revisit the fundamentals. This section of the book is a great start if you have not yet dipped your toes into the magical world of metaprogramming ruby.
I also learned a couple of new things that I wasn’t aware of, including:
- the * splat argument doesn’t have to be the last argument (ruby 1.9).
- the Hash is ordered (ruby 1.9).
- private and protected don’t mean quite what I thought they meant.
- the phrase ‘monkey patching’ might have been derived from ‘guerilla patching’ => ‘gorilla patching’ => ‘monkey patching’
To Summarize
This is a great book for the intermediate ruby developer, its easy to read, clean and concise and covers ruby best practices at 3 different levels (The Basics, Classes and Modules, Metaprogramming). Advanced developers might find it all a bit familiar, but its presented in a way that reinforces those best practices and still a worthwhile read.