From: Adam Bender Date: 2009-05-02T08:49:02+09:00 Subject: Re: Read and re-write file with one open? --001636c5bcc5d48b760468e274ee Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On Fri, May 1, 2009 at 4:33 PM, James Dinkel wrote: > That's pretty close to how I've been modifying files: > > File.open(filename, 'r+') do |file| > lines = file.readlines > > # modify data in the lines array > > file.pos = 0 > file.print lines # will not put \$ between array elements > file.truncate(file.pos) > end > > This opens a file for reading and writing, reads the file into an array, > then you modify the array how you need to. Then the block returns to > the beginning of the file, writes out your changes over the existing > file, then chops off what's left. Then, of course, the file is closed > when the block exits. > This is exactly what I wanted. Thanks everyone. Adam --001636c5bcc5d48b760468e274ee--