module Spruz::Write
Public Instance Methods
write(filename, content = nil, mode = 'w') { |temp| ... }
click to toggle source
Write to a file atomically
# File lib/spruz/write.rb, line 4 def write(filename, content = nil, mode = 'w') temp = File.new(filename + ".tmp.#$$.#{Time.now.to_f}", mode) if content.nil? and block_given? yield temp elsif !content.nil? temp.write content else raise ArgumentError, "either content or block argument required" end temp.close File.rename temp.path, filename ensure temp and !temp.closed? and temp.close File.file?(temp.path) and File.unlink temp.path end