> Interesting. Here's a potential problem. You won't hit
> method_missing for methods that Object has. In particular, a
> client might use dup, ==, to_s, class, kind_of?, and so on.
> You might try getting around some of this by using Delegate
> from the standard library, or Facets' BlankSlate IIRC.

I was aware of this problem and was already working it out. My
solution is to overwrite all (except a few) already defined
methods, so they call method_missing.

> I was thinking three parameters was too many for
> LazyLoad#initialize (just provide a block which loads and
> returns the loaded value), and started to rewrite it, when I
> remembered MenTaLguY's lazy.rb, which does something very
> similar.

Right... Maybe using a block was just to obvious...

New versions below.

(I keep the method Branch#load, because it not only fills
Branch@items, but Branch@snapshots as well.)

Thanks.

More comments? More ideas? More things I overlooked?

gegroet,
Erik V. - http://www.erikveen.dds.nl/

----------------------------------------------------------------

 class LazyLoad
   instance_methods.each do |method_name|
     unless ["__send__", "__id__"].include?(method_name)
       class_eval <<-"EOF"
         def #{method_name}(*parms, &block)
           method_missing(:#{method_name}, *parms, &block)
         end
       EOF
     end
   end

   def initialize(&block)
     @block = block
   end

   def method_missing(method_name, *parms, &block)
     @block.call.send(method_name, *parms, &block)
   end
 end

----------------------------------------------------------------

 class Branch
   def initialize
     @items     = LazyLoad.new{load; @items}
     @snapshots = LazyLoad.new{load; @snapshots}
   end

   def load
     @items     = {}
     @snapshots = {}

     # Fill @items and @snapshots...
     # EXPENSIVE, TIME CONSUMING, MEMORY HUNGRY!
   end
 end

----------------------------------------------------------------