>>>>> "T" == Tim Bates <tim / bates.id.au> writes:

T> I'm trying to build a caching mechanism into a library I'm writing. I
T> want to do it using weakrefs. It will look something like this:

 Perhaps best to don't use weakref. Something like (*this is just an
 example*) 

   class WeakPool
      def initialize
         @obj = Hash.new
      end

      def final(a)
         lambda { @obj.delete(a) }
      end

      def []=(a, b)
         ObjectSpace.define_finalizer b, final(a)
         @obj[a] = b.id
      end

      def [](a)
         b = @obj[a] 
         b = ObjectSpace._id2ref(b) unless b.nil?
         b
      end
   end


Guy Decoux