Ruby Metaprogramming Episode 7: More Hook Methods
We’ll pick up where we left off in the last episode by looking at two more Ruby hook methods: included and method_added. But we’ll also take it a step further. We’ll use these hook methods to develop a metaprogramming library that traces the execution of a Ruby program. Along the way we’ll see all the various subtle (and important!) things you need to think about when you’re trying to write a general-purpose metaprogramming library.
- Using
includedto intercept when a module is included in a class, and use it to set up another hook method in a different context - Using
method_addedto track when a new method is defined on a class, and trace the method’s execution - Refactoring the tracing to support blocks
- Using method objects to bypass naming issues
- Adding tracing to methods that have already been added
- Suppressing tracing for certain methods
- Using
Thread.currentto define thread-local variables - Differences between Ruby 1.8 and 1.9
- Practical examples (and corner cases) of metaprogramming