Created
July 14, 2010 13:25
-
-
Save jyurek/475400 to your computer and use it in GitHub Desktop.
Make any File a temporary file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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