Skip to content

Instantly share code, notes, and snippets.

@croaky
Created October 26, 2008 18:47
Show Gist options
  • Save croaky/19928 to your computer and use it in GitHub Desktop.
Save croaky/19928 to your computer and use it in GitHub Desktop.
require 'fileutils'
require 'tempfile'
class File
def File.open_safely(path)
result, temp_path = nil, nil
Tempfile.open("#{File.basename($0)}-#{path.hash}") do |file|
result = yield file
temp_path = file.path
end
FileUtils.move temp_path, path
result
end
end
# getting RuntimeError: cannot generate tempfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment