class File

Public Class Methods

write_with_backup(path) { |io| ... } 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.

# File lib/inline.rb, line 873
def self.write_with_backup(path) # returns true if file already existed

  # move previous version to the side if it exists
  renamed = false
  if File.file? path then
    begin
      File.rename path, path + ".old"
      renamed = true
    rescue SystemCallError
      # do nothing
    end
  end

  File.open(path, "w") do |io|
    yield(io)
  end

  return renamed
end