From: Daniel Berger Date: 2010-09-14T20:58:37+09:00 Subject: Re: Getting a unique filename? On Sep 14, 4:00 am, Michael Schuerig wrote: > I need to build a Zip-Archive with a unique name that is served by > Apache using its X-SendFile mechanism. > > Tempfile doesn't appear to be suitable in this case as it unlinks its > managed file when the Tempfile object is finalized. In my current, wrong > code, I'm using Tempfile and the file is not deleted prematurely, but I > take this to be no more than luck. > > There are two processes involved, the ruby process (Rails, actually) > writing the file and apache delivering it. Ruby only passes the file's > path to apache. The file's life has to be independent of the ruby > process from then on (and I have to delete it externally). > > I could (ab)use Tempfile to write code like this, but that doesn't > strike me as very elegant > >   tf = Tempfile.new(['archive-', '.zip']) >   tempname = tf.path >   tf.close! > > Is there a nicer way? require 'file/temp' fh = File::Temp.new(false) # Keep the file around Regards, Dan