|Metaprogramming Ruby 2|
Author: Paolo Perrotta
A second edition of a book that claims to teach you "meta" programming. Isn't just programming hard enough?!
You will often hear Ruby programmers say that Ruby is special - this book explains what makes it special and how to make use of these special abilities. It has reached its second edition and it is still the best book on the topic.
It has a strange style that you need to be warned about this. The entire book takes the form of a tutorial between a master programmer called Bill and his increasingly skilled assistant, i.e. you. The task is to build some software for a remarkably knowledgeable boss figure. Unlike most attempts at this method of presenting technical information, it works!
It is sufficiently well done for it to be a help in explaining some fairly complicated ideas. Even so there were times when I really would have liked a quick simple and direct explanation of what is going on. There are plenty of summaries and overviews, but these are generally placed a page or two ahead of the main narrative.
Metaprogramming has long been regarded as dangerous. It is easier to create a metaprogramming mess than it is to create something easy to understand, easy to debug and modify. Most of the time the ideas and techniques are introduced with a correct degree of caution about the impact of metaprogramming on the system as a whole. However, there are moments when you really should worry about letting this particular genie out of the bottle.
The first section of the book works its way though the various standard metaprogramming techniques. Each chapter is given the name of a day of the week and the idea is that it takes you through what was necessary to implement an application. Monday introduces the Ruby object model and provides the foundation that a lot of what follows is built on. Tuesday is all about methods - how Ruby works out which method to call and in what context. Here we meet one of the main tools in the metaprogramming toolkit - missing methods. Wednesday is all about blocks - which is a very Ruby idea. However, you do end up seeing blocks as a much more integral part of the language than they seem when you are first introduced to them. The example used is of a simple Domain Specific Language (DSL) and if you have always thought that DSL were a waste of time be prepared to think again. Thursday is also about Class and how the type system works in Ruby. Friday is hardcore metaprogramming as we finally get to code that writes code - scary stuff made to seem very reasonable.
The second part of the book is about Rails and is the only part of the book that has been heavily changed in the second edition. I starts with a tour of Rails and then moves on to ActiveRecord, which is a core Ruby system that makes heavy use of metaprogramming. The new chapters are on the Concern Module, alias_method_chain, Arttribute methods and a final round out.
The Appendixes are far more useful than you find in most books and are listed as Part III. The first goes over common Ruby idioms and not just metaprogramming idioms. The second is an introduction and advocate of DSLs - if you haven't encountered the ideas before then this is the place to start. The final appendix is a collection of "spells" - small patterns of metaprogramming use. All really useful.
If I have to find a fault with the book then its that it is a bit too enthusiastic about Ruby. If you are a Ruby fan then you will simply agree but if you are trying to see what the fuss is about then it could just put you off. It would have been better to tone it down just a touch.
This is a book that will take you from a Ruby novice to an expert, especially if you have migrated to Ruby from another language and are still using it as if it was another language. Even if you don't want to go too deeply into metaprogramming and don't want to actually make use of these "dangerous" techniques the book will impart enough deep understanding of they way Ruby works and its philosophy for you to be a better Ruby programmer after reading it.
It probably isn't worth buying the new edition if you have the original unless you want to know about the new Rails features.
Not for the complete beginner but for just about every other Ruby programmer - highly recommended.
|Last Updated ( Friday, 29 April 2016 )|