From: Paul Brannan Date: 2001-12-20T23:51:50+09:00 Subject: [ruby-talk:29122] Re: overriding methods: (almost) a replacement for alias_method I think I may have found a solution, using two modules (one containing the original method, and one containing the new method): class Module def override_method(*symbols, &block) tmp_module1 = Module.new tmp_module2 = Module.new include tmp_module2 symbols.each do |symbol| tmp_module1.__send__(:define_method, symbol, instance_method(symbol)) tmp_module2.__send__(:include, tmp_module1) tmp_module2.module_eval(&block) define_method(symbol, tmp_module2.instance_method(symbol)) end end end For example: class Foo def foo puts "foo!" end override_method :foo do def foo puts "foo 2!" super() end end override_method :foo do def foo puts "foo 3!" super() end end end Foo.new.foo #=> foo 3! #=> foo 2! #=> foo 1! There are two big problems, though, with this solution (as dblack pointed out): 1) The original (overriden) foo must not call super(), else it will end up calling "foo 3". 2) The new (overriding) foo's -- "foo 2" and "foo 3" -- cannot call super(), else they will call the the foo that was defined before they were (in "foo 3"'s case, "foo 2", and in "foo 2"'s case, the original foo). I'm beginning to think that RCR#31 (or perhaps a variation thereof) might be a good idea; it would allow binding a private method object to a method, and it could allow for binding documentation to a method (this could even mean that "anonymous" functions could have documentation bound to them). One particular use I might have for it would be to output test documentation when a particular unit test (or end-to-end test) fails. Paul