From: James Coglan Date: 2008-11-22T00:18:09+09:00 Subject: Re: how to wrap method of singleton class ------=_Part_91512_11569999.1227280403797 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline > How do I write a module, so that when included in Foo it will wrap the > method foo so that Foo.foo will print: You can't (at least not without doing some sneaky metaprogramming -- this code is still a work in progress: http://gist.github.com/25104). A class's own methods take precedence over any modules mixed into it. What you can do is reopen the singleton class and store a reference to the old method: class << Foo # Foo singleton class old_foo = Foo.method(:foo) define_method(:foo) do |*args| old_result = old_foo.call(*args) old_result.upcase end end Foo.foo #=> "FOO" You must use define_method for this instead of def. def does not use a block so you won't get a closure, which you need to retain visibility of the old_foo variable. ------=_Part_91512_11569999.1227280403797--