From: Erik Veenstra Date: 2006-01-25T08:43:12+09:00 Subject: Re: LazyLoad > To get around that problem, you'd want to remember the > block's result after calling it the first time (as lazy.rb > does), rather than calling it multiple times. I added some things to make it thread safe (see below). One of them was @real_object. That's what you mean, I think. > You can do this with lazy.rb too: I've never seen lazy.rb before, but by now the packages is already on my desktop. Needs some investigation. Although doing it yourself is a good learning experience indeed. :-) > Since you mentioned you were using this in production, you > really might want to look into using lazy.rb instead. My "real situation" is a shadow repository for CVS. It's not too bad when it dies. Running the conversion job will refill the DB. :-) Thanks. gegroet, Erik V. - http://www.erikveen.dds.nl/ ---------------------------------------------------------------- require "thread" 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(*parms, &block) @parms = parms @block = block @mutex = Mutex.new @evaluated = false @real_object = nil end def method_missing(method_name, *parms, &block) @mutex.synchronize do @real_object = @block.call(*@parms) unless @evaluated @evaluated = true end @real_object.send(method_name, *parms, &block) end end module Kernel def lazy(*parms, &block) LazyLoad.new(*parms, &block) end end ----------------------------------------------------------------