There's also the blank class pattern in Ruby that needs the undef
functionality.
The idea is to strip every method from your new class so that every call you make to it ends in #method_missing
. That way you can implement a real proxy that just shuffles everything through. Implementing the decorator pattern with this is around ten lines of code, no matter how big your target class is.
If you want to see that idiom in action look at one of Ruby's mocking frameworks, they use it alot. Something like flexmock.
Another example is when you add functions dynamicly onto a class based on some condition. In a game you might add an #attack
method onto the player object and then take it away when he′s paralyzed instead of doing it with a boolean flag. That way the calling class can check for the availabty of the method and not the flag. I′m not saying that this is a good idea, just that it′s made possible and there are far smarter people then me coming up with useful stuff to do with this.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…