Skip to content

Instantly share code, notes, and snippets.

@jyurek
Created July 14, 2010 13:25
Show Gist options
  • Save jyurek/475400 to your computer and use it in GitHub Desktop.
Save jyurek/475400 to your computer and use it in GitHub Desktop.
Make any File a temporary file
require 'fileutils'
class File
def self.finalizer(path)
lambda do
FileUtils.rm(path)
end
end
def tempify!
ObjectSpace.define_finalizer(self, &self.class.finalizer(self.path))
end
end
10.times do |n|
File.open("/tmp/blah#{n}.out", "w") do |f|
puts "This is file #{n+1}"
f.tempify!
f.puts "omg"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment