Ruby Metaprogramming Episode 6: Some Hook Methods
Ruby hook methods are a way for your application to hook itself into the execution of the Ruby interpreter. Using hook methods is crucial for some kinds of metaprogramming, and they can make your code more flexible. In this episode, we’ll see how to use two powerful hook methods: inherited and const_missing.
- Overriding hook methods to intercept and deal with certain Ruby interpreter events during the lifetime of your application
- Decoupling code using
inheritedto keep track of subclasses - Implementing enumerated types with
const_missing - Using
const_missingto autoload classes based on the names of files - Applying
const_missingin both global and localized situations - How to chain an overridden hook method to its original hook method
- Using
const_setto define constants - Two practical uses for hook methods