On Jan 24, 2006, at 9:51 AM, Robert Klemme wrote:

> Can't you just do something like this?
>
> require 'delegate'
> require 'weakref'
> WeakHash = DelegateClass(Hash)
> class WeakHash
>   def []=(key,val)
>     __getobj__[WeakRef.new(key)] = WeakRef.new(val)
>   end
>
>   def [](key)
>     __getobj__[WeakRef.new(key)]
>   end
>
>   def each(&b)
>     __getobj__.each do |k,v|
>       b[k.__getobj__, v.__getobj__] unless k.__getobj__.nil?
>     end
>     self
>   end
>
>   def cleanup
>     delete_if {|k,v| k.__getobj__.nil?}
>   end
> end

I didn't see this message while the gateway was down.  Sorry about that.

I can't use this code as is.  I assume you didn't set up delegation  
quite right:

class WeakHash < DelegateClass(Hash)
   def initialize
     super(Hash.new)
   end

   # ...
end

Some questions the above raises for me:

1.  What will Ruby do if a key is GCed, but not a value?
2.  How does this code deal with a value being GCed when the key  
remains?
3.  Don't we need to shut off GC in places, to keep references from  
disappearing before we can use them?

Thanks for the help.

James Edward Gray II