From: James Coglan Date: 2008-11-15T07:02:27+09:00 Subject: Overriding methods in a class using a mixin ------=_Part_14068_6691660.1226700027451 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi list, I just had this idea and thought I'd share it in case someone can improve upon it, make it more robust. Basically, I've run into a situation a few times where I want to override methods in a class but retain access to old implementations, the sort of thing people like to use alias_method_chain for. I tend to favour this approach, though: class Foo meth = instance_method(:foo) define_method(:foo) do |some, args| result = meth.bind(self).call(some, args) # do extra stuff... end end But, then you've lost the old implementation: it's hidden in the closure and you can't get another reference to it from anywhere else. So, I've come up with a way in which you can copy the class's methods into a module, include that module (making it part of the inheritance chain) and then mix other modules in. Later modules can thereby override the class's own methods and use 'super' to refer to them. This lets you insert code between an existing class and all its subclasses, which can be useful. Code on github, with an example: http://gist.github.com/25104 Comments, suggestions, and accusations of idiocy all very much welcome. Expanding from this, I'd like to figure out how I can insert modules at arbitrary points in the inheritance chain, rather than just using 'include' to come between a module and it's last included module. -- James Coglan Lead JavaScript Developer theOTHERmedia http://ojay.othermedia.org +44 (0) 7771512510 ------=_Part_14068_6691660.1226700027451--