From: Joel VanderWerf Date: 2007-06-12T05:03:54+09:00 Subject: Re: Ruby way to update a file in place Li Chen wrote: > Hi all, > > I just wonder what is the Ruby way to update a file in place. Do I need > to open a temporary/new file for writing and then rename the temporary > file? You can open with the "r+" file mode, and use seek/rewind to move to a position within the file, use #truncate to end the file, and so on. [~/tmp] cat >foo foo bar baz [~/tmp] irb irb(main):001:0> f = File.open("foo", "r+") => # irb(main):002:0> f.puts "FOO" => nil irb(main):003:0> f.seek 8 => 0 irb(main):004:0> f.puts "BAZ" => nil irb(main):005:0> f.rewind => 0 irb(main):006:0> f.read => "FOO\nbar\nBAZ\n" irb(main):007:0> f.close => nil irb(main):008:0> [~/tmp] cat foo FOO bar BAZ -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407