Guest User

Untitled

a guest
Dec 14th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. # TempfileEnsureCloser.new.begin do |tmp|
  2. # gz = tmp.create(["tmp", ".gz"]) # File instance
  3. # get_object(gz)
  4. # csv = tmp.create(["tmp", ".csv"])
  5. # system("gunzip -c #{gz.path} > #{csv.path}")
  6. # end #=> close and unlink all temp file
  7. class TempfileEnsureCloser
  8. def initialize
  9. @tempfiles = []
  10. end
  11.  
  12. def begin
  13. yield self
  14. ensure
  15. @tempfiles.each do |t|
  16. t.close
  17. File.unlink(t.path) if File.exists?(t.path)
  18. end
  19. end
  20.  
  21. def create(*args)
  22. t = Tempfile.create(*args)
  23. @tempfiles << t
  24. t
  25. end
  26. end
Add Comment
Please, Sign In to add comment