class File

Public Class Methods

safe_write(dest, txt=nil, &block) click to toggle source
# File lib/util/ruby_extensions.rb, line 93
def File.safe_write(dest, txt=nil, &block)
  tmp = dest + "-" + rand(9999).to_s
  begin
    File.open(tmp, "w") do |file|
      if block
        block.call(file)
      elsif txt
        file.write(txt)
      end
    end
    FileUtils.mv(tmp, dest)
  rescue Exception
    FileUtils.remove_entry_secure(tmp)
    raise
  end
end