2

Dealing with the lifecycle of a Tempfile

 2 years ago
source link: https://dev.to/olistik/dealing-with-the-lifecycle-of-a-tempfile-1lfk
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Cover image for Dealing with the lifecycle of a Tempfile

Dealing with the lifecycle of a Tempfile

Jul 23

・1 min read

When dealing with Tempfiles in Ruby it's important to remember that when a tempfile object gets deallocated, the referenced file gets deleted as well.

require 'tempfile'

def foo
  Tempfile.new.path
end

path = foo # => "/tmp/20210723-30107-1h52x7" 

# Soon after:

File.exists?(path) # => true

# But wait for it..

File.exists?(path) # => false 
Enter fullscreen modeExit fullscreen mode

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK