Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # TempfileEnsureCloser.new.begin do |tmp|
- # gz = tmp.create(["tmp", ".gz"]) # File instance
- # get_object(gz)
- # csv = tmp.create(["tmp", ".csv"])
- # system("gunzip -c #{gz.path} > #{csv.path}")
- # end #=> close and unlink all temp file
- class TempfileEnsureCloser
- def initialize
- @tempfiles = []
- end
- def begin
- yield self
- ensure
- @tempfiles.each do |t|
- t.close
- File.unlink(t.path) if File.exists?(t.path)
- end
- end
- def create(*args)
- t = Tempfile.create(*args)
- @tempfiles << t
- t
- end
- end
Add Comment
Please, Sign In to add comment