2006/1/24, Mauricio Fernandez <mfp / acm.org>:
> I summarized the solutions seen in this thread so far, and presented a couple
> new ones at
>   http://eigenclass.org/hiki.rb?weakhash+and+weakref
>
> There are other ways to implement WeakHash, I'll probably add some to the
> above page (later).

Nice work. Btw, it's expected that my lookup code is slow because it
has to create a weakref for every insertion.  We could try to optimize
this by providing a single weakref for the hash that is used for
lookups.

class WR < WeakRef
  attr_accessor :__getobj__
end

class RKWeakHash
  def initialize(...)
    @lookup = WR.new nil
  end

  def [](x)
     @lookup.__getobj__ = x
    __getobj__[@lookup]
  end
end

Unfortunately this is not thread safe, unless one invests more and
stores lookup thread local...

robert
--
Have a look: http://www.flickr.com/photos/fussel-foto/