On Wed, Jan 25, 2006 at 01:27:25AM +0900, James Edward Gray II wrote:
> Daniel Berger and I have been having an off-list discussion about his  
> memoize.rb library. 
[...]
> The primary difference between our two approaches is that Daniel's  
> library is built to memoize individual objects, while mine is  
> intended to link all instance method calls for a class of objects to  
> a single cache.

I also generalized Daniel's memoize.rb to support class-level
memoization some time ago[1]:

 http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/173587

Should I also release it? ;)

> class Fibonacci
>   extend Memoizable
> 
>   def fib( num )
>     return num if num < 2
>     fib(num - 1) + fib(num - 2)
>   end
>   memoize :fib, FileCache.new("fib_cache.txt")
> end

I kept memoize.rb's interface, but I think I prefer this approach; maybe
special-casing for strings could make sense though...

> You can find an example using weak references and the actual library  
> code in the "Memoization" section of the following article from my blog:
> 
> http://blog.grayproductions.net/articles/2006/01/20/caching-and-memoization
> 
> The point of posting all this here is to give people a chance to  
> express concerns over my implementation.  Daniel was avoiding going  
> down this road because of issues raised by this community.  Raise  
> away.  ;)

I'm not sure about the 
  ([Class, Module].include?(self.class) ? self : self.class)
part; that way, you cannot memoize singleton methods from Module/Class
objects. In my implementation, I distinguished between
Module#instance_memoize and Object#memoize (after including Memoize).

Also,
    original =   "_#{name}"
would need a longer prefix IMO.

Finally, not that it really matters, but the WeakCache is fairly
inefficient (one lambda per key); see that other thread (WeakRef hash)
or
  http://eigenclass.org/hiki.rb?weakhash+and+weakref
for other implementations.

[1] IIRC persistent caching in Daniel Berger's memoize.rb was inspired by
something I wrote, I did a sort of rewrite of memoize.rb and now see my name
mentioned in your blog; it seems memoize.rb and I are bound by the string of
destiny (*^_^*)

-- 
Mauricio Fernandez