Read, edit, and write a text file line-wise using Ruby

In general, there’s no way to make arbitrary edits in the middle of a file. It’s not a deficiency of Ruby. It’s a limitation of the file system: Most file systems make it easy and efficient to grow or shrink the file at the end, but not at the beginning or in the middle. So you won’t be able to rewrite a line in place unless its size stays the same.

There are two general models for modifying a bunch of lines. If the file is not too large, just read it all into memory, modify it, and write it back out. For example, adding “Kilroy was here” to the beginning of every line of a file:

lines = IO.readlines(path).map do |line|
  'Kilroy was here ' + line
end, 'w') do |file|
  file.puts lines

Although simple, this technique has a danger: If the program is interrupted while writing the file, you’ll lose part or all of it. It also needs to use memory to hold the entire file. If either of these is a concern, then you may prefer the next technique.

You can, as you note, write to a temporary file. When done, rename the temporary file so that it replaces the input file:

require 'tempfile'
require 'fileutils'

temp_file ='foo')
begin, 'r') do |file|
    file.each_line do |line|
      temp_file.puts 'Kilroy was here ' + line
  temp_file.close, path)

Since the rename ( is atomic, the rewritten input file will pop into existence all at once. If the program is interrupted, either the file will have been rewritten, or it will not. There’s no possibility of it being partially rewritten.

The ensure clause is not strictly necessary: The file will be deleted when the Tempfile instance is garbage collected. However, that could take a while. The ensure block makes sure that the tempfile gets cleaned up right away, without having to wait for it to be garbage collected.

Leave a Comment