From: Erik Veenstra Date: 2006-01-26T03:03:14+09:00 Subject: Re: LazyLoad > Nuking data.txt is probably not the right response to a > missing mapping.txt... I got it. I introduced LazyException (see below) as a wrapper for the original Exception. All methods are (once again) delegated to the original exception with method_missing, except for LazyException#exception. (By the way, in your example, you use an exception to do some business logic: "If the file doesn't exist, create it". In OO theory, this isn't the right way to go...) What more? gegroet, Erik V. - http://www.erikveen.dds.nl/ ---------------------------------------------------------------- class LazyException < Exception superclass.instance_methods(false).each do |method| undef_method(method) unless method =~ /^__/ end def initialize(exception) @original_exception = exception end def exception self end def method_missing(method_name, *parms, &block) @original_exception.send(method_name, *parms, &block) end end class Lazy instance_methods(true).each do |method| undef_method(method) unless method =~ /^__/ end def initialize(mutex=Mutex.new, &block) @mutex = mutex @block = block @evaluated = false @exception = nil @real_object = nil end def method_missing(method_name, *parms, &block) @mutex.synchronize do begin @real_object = @block.call() unless @evaluated rescue Exception => e @exception = LazyException.new(e) ensure @evaluated = true end end raise @exception if @exception @real_object.send(method_name, *parms, &block) end end module Kernel def lazy(*parms, &block) Lazy.new(*parms, &block) end end ----------------------------------------------------------------