On Jan 24, 2006, at 1:06 PM, Robert Klemme wrote: >> I can't use this code as is. I assume you didn't set up delegation >> quite right: > > What problem exactly do you have with it? When I placed the code you posted in a file and tried to create WeahHash, the program crashed. (Wrong number of arguments to initialize().) I assume it would have worked if I passed the Hash manually though. >> class WeakHash < DelegateClass(Hash) >> def initialize >> super(Hash.new) >> end >> >> # ... >> end > > No need to inherit to fix this. You can simply do > > require 'delegate' > Wh=DelegateClass(Hash) > class Wh > def initialize(h={}) > __setobj__ h > end > end Here's another question for you: What are we gaining by delegating to an object here, as opposed to subclassing? >> Some questions the above raises for me: >> >> 1. What will Ruby do if a key is GCed, but not a value? > > This was just rough outline code. For a production version I'd > probably change it to consider a pair as gone if either of them is > GC'ed. > >> 2. How does this code deal with a value being GCed when the key >> remains? > > It will yield nil - but see 1. > >> 3. Don't we need to shut off GC in places, to keep references from >> disappearing before we can use them? > > No. Because while the yield takes place instances are hard referenced > and cannot be gc'ed. Good answers all around. Thanks. James Edward Gray II