Skip to content

Instantly share code, notes, and snippets.

@isidzukuri
Created July 20, 2020 12:09
Show Gist options
  • Select an option

  • Save isidzukuri/6aa917662183967dccf427d2524de8bb to your computer and use it in GitHub Desktop.

Select an option

Save isidzukuri/6aa917662183967dccf427d2524de8bb to your computer and use it in GitHub Desktop.
creates temporary file with name without randomizing
class WriteTemporaryFile
class Remover
def initialize(tmpfile)
@pid = Process.pid
@tmpfile = tmpfile
end
def call(*args)
return if @pid != Process.pid
@tmpfile.close
begin
File.unlink(@tmpfile.path)
rescue Errno::ENOENT
end
end
end
def self.call(filename, content)
file = File.new("tmp/#{filename}", 0|File::RDWR|File::CREAT|File::EXCL, {perm: 0600})
file.binmode
file.write(content)
ObjectSpace.define_finalizer(self, Remover.new(file))
file.read
file
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment