Monday, December 7, 2009

A Ruby book worth reading for Pythonistas

It's been a while since I've ready an entire technical book over the weekend. But I enjoyed Design Patterns in Ruby so much that I kept picking it up for "one more chapter".

I played with Ruby a little several years ago, but I've never used it seriously. I need to use it at work though. Specifically, I wanted to learn about creating Domain-Specific Languages (DSLs) in Ruby, which is what led me to this book (since it has a chapter on DSLs).

When the book arrived late Thursday I skipped straight to the DSL chapter, which was interesting. But then I started reading it from the beginning. Chapter 1 talks about design patterns, the book Design Patterns, and the general principles that begin the Gang of Four book:
  • Separate out the things that change from those that stay the same.
  • Program to an interface, not an implementation.
  • Prefer composition over inheritance.
  • Delegate, delegate, delegate.
Russ Olsen adds "You ain't gonna need it."

Chapter 2 is a concise introduction to Ruby: just 37 pages. It's not thorough, but it's enough to be able to read the Ruby code in the subsequent chapters.

Chapters 3 through 15 explain many of the patterns covered in the Gang of Four book. I enjoyed brushing up on my patterns, but what kept me wanting to read "one more chapter" over the weekend was how after explaining the pattern, Olsen explains how it does or does not apply in Ruby. And he takes the opportunity to introduce Ruby features (like procs & blocks, modules and mixins, modifying classes and objects on the fly, message passing, etc.) as he does this.

He also write about how each pattern can be abused, and describes real-world uses of the patterns in the Ruby libraries.

Chapters 16 through 18 describe three "patterns that have emerged with the introduction and expanded use of Ruby": DSLs, meta-programming and convention over configuration. And there are two appendices: A on installing Ruby on different platforms, and B contains a carefully annotated bibliography.

I suspect I'll find myself referring back to this book often, even when I'm writing Python code. I suspect reading it will help make me a better Python programmer (and a better programmer in general). So I recommend it.

If I had loads of time, it would be a worthwhile exercise to go through each pattern in the book and convert the examples to Python, and look for examples of the pattern in the Python libraries. I wonder if anyone else with more time has already had this idea.