From: Robert Klemme Date: 2004-03-05T20:09:43+09:00 Subject: Re: rescue inside block -> syntax error "Joe Mason" schrieb im Newsbeitrag news:slrnc4gj4e.9vj.joe@gate.notcharles.ca... > In article , Robert Klemme wrote: > > There's an issue that limits the usability of do..ensure..end: Typically > > you do resource allocation *outside* the begin..end because you don't want > > the ensure clause to be executed if something during init fails: > > > > container.each do |obj| > > processor = create_processor_in_a_way_that_might_throw() > > > > begin > > processor.do_work() > > processor.do_more_work() > > ensure > > processor.cleanup() > > end > > end > > But wouldn't the ruby way be to write this? No, because that would duplicate cleanup code. > container.each do |obj| > processor.create_in_a_way_that_might_throw do |proc| I gues you rather mean ProcessorClass.create_in_a_way_that_might_throw do |proc| > proc.do_work() > proc.do_more_work() > ensure > proc.cleanup() The cleanup wrong here because it is done in method create_in_a_way_that_might_throw() in this scheme after the yield (see File.open() and below). > end > end > > Joe And how then will you implement create_in_a_way_that_might_throw()? That's the second example I gave - slightly changed to fit the naming: def create_in_a_way_that_might_throw() # initialization, might throw processor = ... begin yield processor ensure processor.cleanup() end end Regards robert