Good artists copy, great artists steal.
In the first part of this book, you spent a week brushing elbows with another coder and making your way through the internals of Ruby. You also filled your toolbox with magic metaprogramming tricks, such as Dynamic Methods () and Class Macros ().
So, you’ve got the know-how and the tools. But now you’re wondering how to combine them into real-life code. How can you keep your Open Classes () under control? When should you use a Ghost Method () rather than a Dynamic Method ()? How do you test your Class Macros ()? To answer these kinds of questions, you need more than knowledge and tools. You need experience.
You can’t get experience simply by reading a book, but you can get a lot of value out of looking at the work of experienced coders. The short chapters in this second part of the book take you on a tour through the source code of Ruby on Rails (or just “Rails,” for short), the quintessential Ruby project. Rails’ code uses metaprogramming at every step and is often more complex than any code you’ve seen so far in this book. Because of that complexity, it’s a great example of both the power of metaprogramming and its potential dangers.
Rather than an exhaustive exploration of Rails, this tour is like a sightseeing excursion on one of those open-air, double-decker buses. I’ll trace a few scenic routes through the Rails source code, and in the process show you how some of the best Ruby programmers apply metaprogramming spells to solve real-life problems. But first, let’s talk about Rails itself.