class File

Public Class Methods

write_with_backup(path, content) click to toggle source

Equivalent to File::open with an associated block, but moves any existing file with the same name to the side first. Returns renamed path or false if none.

# File lib/inline.rb, line 858
def self.write_with_backup(path, content)
  renamed = false

  if File.file? path then
    renamed = "#{path}.old"

    begin
      File.rename path, renamed
    rescue SystemCallError # in case of race condition
      # do nothing
    end
  end

  File.write path, content

  renamed
end