Sha256: c8d04760fde7b4d812906e2d983b9ec078c6a14dfaaab5a826c62c78e8fa4a81

Contents?: true

Size: 601 Bytes

Versions: 1

Compression:

Stored size: 601 Bytes

Contents

module Memoize
   # The version of the memoize library
   MEMOIZE_VERSION = '1.3.1'
   
   # Memoize the method +name+.  If +file+ is provided, then the method results
   # are stored on disk as well as in memory.
   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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
memoize-1.3.1 lib/memoize.rb