posted 17 years ago
So, let's say you have a class that's buried somewhere, like, Rails' ActionController. And you want to add a new method to it, like, "help" (so that all your controllers had a /your_controller/help url that tells what that controller is for).
You can create a module to hold the new, shared method:
Then, you can, from anywhere in the code, call:
Voila. Your controller now has a "help" method.
What if you wanted to wrap a method already in a class? For that, you would use .
If you then extend that into a class with a "save" method, it renames the original method, creates a new method of the same name (a proxy, if you will), and when it is called, the new method logs the process and forwards the call to the original method.
There is a new addition to Rails 1.2 called alias_method_chain which makes the whole process a little cleaner, as well.
[ January 31, 2007: Message edited by: Justin Gehtland ]