class Module

def memoize_method(*method_ids)

object the method is called on.
memoized results do NOT ONLY depend on the arguments, but ALSO on the
Automatically memoize calls of the the methods +method_ids+. The
def memoize_method(*method_ids)
  method_ids.each do |method_id|
    method_id = method_id.to_s.to_sym
    orig_method = instance_method(method_id)
    __send__(:define_method, method_id) do |*args|
      unless mc = ::Module.__memoize_cache__[__id__]
        mc = ::Module.__memoize_cache__[__id__] ||= {}
        ObjectSpace.define_finalizer(self, ::Module.__memoize_cache_delete__)
      end
      if mc.key?(method_id) and result = mc[method_id][args]
        result
      else
        (mc[method_id] ||= {})[args] = result = orig_method.bind(self).call(*args)
        $DEBUG and warn "#{self.class} cached method #{method_id}(#{args.inspect unless args.empty?}) = #{result.inspect} [#{__id__}]"
      end
      result
    end
  end
end