As we’ve seen, metaprogramming in Ruby often involves
the dynamic definition of methods.
Just as common is the dynamic modification of methods.
Methods are modified with a technique we’ll call alias chaining.
It works like this:
Note that these steps can be applied repeatedly (as long as a different
alias is used each time), creating a chain of methods and aliases.
- First, create an alias for the method to be modified. This alias provides a name for the unmodified version of the method.
- Next, define a new version of the method. This new version should call the unmodified version through the alias, but it can add whatever functionality is needed before and after it does that.
Casiano Rodriguez León