From: "Steve [RubyTalk]" Date: 2005-12-02T04:21:38+09:00 Subject: Re: What is the best way to edit a file to eliminate a line 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. :-)