Ruby Metaprogramming Episode 6: Some Hook Methods

Posted in Development on July 17, 2008

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 inherited to keep track of subclasses
  • Implementing enumerated types with const_missing
  • Using const_missing to autoload classes based on the names of files
  • Applying const_missing in both global and localized situations
  • How to chain an overridden hook method to its original hook method
  • Using const_set to define constants
  • Two practical uses for hook methods

Watch Video

Tags: Ruby, Metaprogramming, The Pragmatic Programmers, Ruby Metaprogramming