module Memoize

Constants

MEMOIZE_VERSION

The version of the memoize library

Public Instance Methods

memoize(name, file=nil) click to toggle source

Memoize the method name. If file is provided, then the method results are stored on disk as well as in memory.

Calls superclass method
# File lib/memoize.rb, line 7
def memoize(name, file=nil)
   cache = File.open(file, 'rb'){ |io| Marshal.load(io) } rescue {}

   (class<<self; self; end).send(:define_method, name) do |*args|
      unless cache.has_key?(args)
         cache[args] = super(*args)
         File.open(file, 'wb'){ |f| Marshal.dump(cache, f) } if file
      end
      cache[args]
   end
   cache
end