Steve [RubyTalk] wrote: > zdennis wrote: > >>> def write filename, data >>> File.open( filename, 'w' ){ |file| file.write data } >>> end >>> >>> begin >>> data = IO.read( 'stuff.txt' ) >>> write 'stuff.txt', data.gsub( /^foo(\n|$)/, '' ) >>> rescue >>> write 'stuff.txt.orig', data >>> end >>> >> You'd probably want the rescue statement to be a "rescue Exception" so >> you catch any/all errors... > > Both versions look dangerous to me. > > 1. If an exception is raised on opening 'stuff.txt' to read then an > attempt will be made to truncate the file (or to overwrite it with > whatever happened to be in data previously. [This could be avoided by > reading before begin.] > > 2. If a disk becomes full (or nearly full) during the write operation > then the rescue will likely not be able to write all the unmodified data > back - hence permanently loosing valuable information. > > I need a more robust approach than this. :-) > Understood. I don't know full extent of your issue. It appears you can run into a lot of possibilities regarding where the *power goes up*. It could happen during any system process, not just rubys. If this helps lead you to an elegant implementation, great! Otherwise...maybe it will steer you away from a potential disaster! good luck! Zach