
module Kernel

  # Object#cache is essentially like Module#memoize except
  # it can also be used on singleton/eigen methods.
  # OTOH, memoize's implementation is arguably better for it's
  # use of #bind instead of #alias. Eventually the two implmenations
  # will be reconciled with a single implmentation.

  def cache m = nil
    if m
      (Module === self ? self : (class << self; self; end)).module_eval <<-code
        alias_method '__#{ m }__', '#{ m }'
        def #{ m }(*__a__,&__b__)
          c = cache['#{ m }']
          k = [__a__,__b__]
          if c.has_key? k
            c[k]
          else
            c[k] = __#{ m }__(*__a__,&__b__)
          end
        end
      code
    end
    @cache ||= Hash::new{|h,k| h[k]={}}
  end

end



