From: Robert Klemme Date: 2003-05-21T17:56:34+09:00 Subject: Re: An Object Going Out Of Scope "Mauricio Fern�ndez" schrieb im Newsbeitrag news:20030521055137.GA6046@student.ei.uni-stuttgart.de... > On Wed, May 21, 2003 at 05:14:06AM +0900, MikkelFJ wrote: > > > > "Jim Weirich" wrote in message > > news:1053406677.17559.8.camel@traken... > > > > > How about this syntax ... > > > > > > auto(foo=MyObject.new, :cleanup) do > > > # use foo > > > end > > > > fine if you can handle multiple objects this way. Mauricios suggestion does > > that, but does not allow you to specify cleanup method. This, of course can > > be done in many ways. > > It seems you overlooked the fact that it is not implementable in current > Ruby, for it needs Procs to be rebound :) I'm not sure, whether I understand you correctly. The solution below does work. Why then is it not possible without rebinding procs? > It'd be easy to change it to specify the cleanup method, should it run > at all. > > def auto(hash) > .. > end > > and then > > auto( :a => :finalize, :file => close ) do > file = File.new("/tmp/bla") > a = Something.new > end How about: def auto2( hash ) begin yield ensure hash.each do |obj, method| obj.send method end end end class Foo def cleanup puts "cleanup" end end auto2( Foo.new => :cleanup ) do puts "in block" end robert