From: Dave Burt Date: 2006-01-25T00:58:51+09:00 Subject: Re: LazyLoad Erik Veenstra wrote: > ... > Comments? Ideas? Something I overlooked? > ... > class LazyLoad > def initialize(object, load_method, property) > @object = object > @property = property > @load_method = load_method > end > > def method_missing(method_name, *parms, &block) > @object.send(@load_method) > @object.instance_eval("@#{@property.to_s}").send(method_name, > *parms, &block) > end > end 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 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. STEP 4, INTRODUCING lazy.rb class Branch def initialize @items = promise do @items = {} # Fill @items... EXPENSIVE, TIME CONSUMING, MEMORY HUNGRY! @items end end end Cheers, Dave