From: "ara.t.howard" Date: 2008-09-23T04:54:02+09:00 Subject: Re: at_exit handler *except* for fatal runtime error On Sep 22, 2008, at 12:36 PM, DMisener wrote: >>> >>> > output=TentativeFile.new.open('temp.tmp','w') > output.puts 'Thanks' > > without an explict close doesn't automatically rename the file to its > proper form. > > With "regular" File.open ... the file is automatically closed without > need of explicit close. That was > the behaviour I was trying to emulate. I'm starting to think this > "frill" is a lost cause. > > you've got to play with code at least a little! cfp:~ > cat a.rb class TentativeFile < File Ext = 'tentative' def initialize path, *a, &b super "#{ path }.#{ Ext }", *a, &b at_exit{ close unless closed? } unless b end def close STDERR.puts "closing #{ path }..." final = path.to_s.sub %r/[.]#{ Ext }$/, '' super ensure File.rename path, final unless $! end end output=TentativeFile.new('temp.tmp','w') output.puts 'Thanks' cfp:~ > ruby a.rb closing temp.tmp.tentative... cfp:~ > cat temp.tmp Thanks a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama