No method_added
hook implementation leads to proper working with include
'ed methods.
Use prepend
from Ruby 2.0. Decorators should return Module
s as results of calling new
:
class Memoize
def self.new(method)
m = Module.new
m.define_method(method) do
# memoization logic goes here.
# use `super` to call original method.
end
m
end
end
@michaelfairley - I was just focused on the case when the includer is doing the decoration. The simplicity of this version is possible only due to powerful nature of
prepend
which is a new brother of our belovedinclude
. I can't wait to see what Ruby community is going to do with it.