A review of Metaprogramming Ruby


I want to say it straight away: you should read this book. I’m reading a bunch of Ruby-related books and this is one of the best available on the market. So, in this review, I’ll try to give an overview of the book still I guess I won’t be good enough to explain how good this book is.

The book #

The aim of Metaprogramming Ruby is very clear and simple. One of its goals is to cover all the aspects of metaprogramming related to Ruby. Furthermore it aims to cover some of the most interesting techniques that make Ruby so fascinating. The book starts with a nice introduction on the reasons why you should read it and then introduces your fellow Meta-programmer to you - Bob. I confess that when I met Bob for the first time I was a bit sceptical about the expedient. It seems a bit simplistic approach but I assure you that Paolo Perrotta has done a terrific job. All the examples related to Bob the meta-programmer are very intelligent and well-made. While reading the book you will occasionally run into interesting techniques that the author calls “spells”. I find it a very nice way to call them. This is a great idea, it helps you to understand better the entire Ruby meta-programming eco-system.

My considerations grouped by chapter:

Monday: The Object Model #

This book is not intended for beginners, at least not for absolute ones, but this chapter is so clear, simple and enjoyable to read that I would recommend reading it to everyone. It is a very readable introduction to how modules, classes and objects are meant to be in Ruby. Furthermore, this chapter contains the best definition of the private Ruby keyword I have ever read.

Tuesday: Methods #

The chapter covers some of the most fascinating aspects of metaprogramming. It starts with a nice example of how some simple Ruby features can be used to refactor a class in a very elegant way. The main topics of the chapter are Dynamic Methods and method_missing. Both topics are very well-covered. At the end of the chapter the author warns you about some problems you might face while using the techniques mentioned above. He uses nice examples to explain these issues.

Wednesday: Blocks #

The chapter starts with a general introduction to blocks and goes on with various types of scope, procs and lambdas. It presents a very good DSL example, used to explain how instance_eval works. Surely, the chapter is worth reading.

Thursday: Class Definitions #

This is one of the most important chapters of the book, maybe it is actually the most important one. I’m still impressed by its clarity and I often reread chunks of it. In this chapter Paolo Perrotta uses very good definitions like the one of “current class”. I have to say that one of the most notable aspects of the book is the perfect choice of definitions for “wild” Ruby features. Furthermore you’ll find a very great explanation of the eigenclass. I have to warn you: if you aren’t an expert, this chapter will wear you out, but don’t worry, you will be satisfied when you finish it. This chapter contains a great number of topics, all of them well-covered. I think you should read it as I do: many times.

Friday: Code That Write Code #

The chapter covers very fascinating topics. You’ll learn important features of Ruby as Kernel#eval. A very fitting example comes through all the parts of the chapter. This is one of the most funny chapters of the book.

The design of ActiveRecord #

The chapter is a tour of how ActiveRecord is designed. It’s is a nice introduction to the topic. It can be also a good way to refresh your knowledge of it.

Inside ActiveRecord #

ActiveRecord is a great framework. Many of us, Ruby programmers, approach this wonderful language through Rails and ActiveRecord is one of the most beautiful part of ror. It contains many features that for programmers new to Ruby look like magic. The author wrote a simple introduction to topics like Dynamic Finders and Dynamic Attributes. If you wonder how on earth these great features work, you’ll get a good picture of them.

Metaprogramming Safely #

This chapter will give you good advice on how to write metaprogramming code without losing it. It covers aspects like tests and correct monkey patching.

At the end, the book contains 3 appendixes. They are all very readable and interesting, the appendix about Ruby common idioms is my favourite.

What I liked most #

Generally speaking, I prefer description oriented books rather than example oriented ones, but this book changed my mind about that. The author is so good at writing examples that I think you’ll love it as well. If you are searching for a book to help you make a step further in your Ruby learning, stop it. You have just found it.

What I disliked most #

Since all my reviews contain this paragraph I won’t leave it out this time, even thought there is absolutely nothing I disliked in this book.