# Getting a unique filename?

**URL:** <https://www.ruby-forum.com/t/getting-a-unique-filename/194960>\
**Category:** Ruby\
**Created:** [September 14, 2010, 12:01pm UTC](https://www.ruby-forum.com/t/getting-a-unique-filename/194960 "2010-09-14T12:01:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [September 14, 2010, 12:01pm UTC](https://www.ruby-forum.com/t/getting-a-unique-filename/194960/1 "2010-09-14T12:01:41Z")

</div>

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?

Michael

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [September 14, 2010, 2:00pm UTC](https://www.ruby-forum.com/t/getting-a-unique-filename/194960/2 "2010-09-14T14:00:30Z")

</div>

On Sep 14, 4:00 am, Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [September 14, 2010, 2:15pm UTC](https://www.ruby-forum.com/t/getting-a-unique-filename/194960/3 "2010-09-14T14:15:44Z")

</div>

Daniel B. wrote:

> > 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

Thanks, that looks good, but unfortunately doesn’t work for me. See  
below for the trace. Apparently it stumbles because the content of  
/usr/libc.so is not as expected. I’m running Debian/unstable amd64.

Michael

# /usr/lib/libc.so

/\* GNU ld script  
Use the shared library, but some functions are only in  
the static library, so try that secondarily. \*/  
OUTPUT\_FORMAT(elf64-x86-64)  
GROUP ( /lib/libc.so.6 /usr/lib/libc\_nonshared.a AS\_NEEDED ( /lib/ld-  
linux-x86-64.so.2 ) )

$ rake test  
(in /var/lib/gems/1.8/gems/file-temp-1.1.2)  
/usr/bin/ruby1.8 -w -I"lib" “/usr/lib/ruby/1.8/rake/rake\_test\_loader.rb”  
“test/test\_file\_temp.rb”  
/var/lib/gems/1.8/gems/ffi-0.6.3/lib/ffi/library.rb:61:in `ffi_lib': Could not open library 'libc': libc: cannot open shared object file: No such file or directory. Could not open library 'libc.so': /usr/lib/libc.so: invalid ELF header (LoadError) from /var/lib/gems/1.8/gems/ffi-0.6.3/lib/ffi/library.rb:43:in`map’  
from /var/lib/gems/1.8/gems/ffi-0.6.3/lib/ffi/library.rb:43:in  
`ffi_lib' from ./lib/file/temp.rb:47 from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in`gem\_original\_require’  
from /usr/lib/ruby/1.8/rubygems/custom\_require.rb:31:in  
`require' from ./test/test_file_temp.rb:11 from /usr/lib/ruby/1.8/rake/rake_test_loader.rb:5:in`load’  
from /usr/lib/ruby/1.8/rake/rake\_test\_loader.rb:5  
from /usr/lib/ruby/1.8/rake/rake\_test\_loader.rb:5:in `each’  
from /usr/lib/ruby/1.8/rake/rake\_test\_loader.rb:5  
rake aborted!  
Command failed with status (1): [/usr/bin/ruby1.8 -w -I"lib"  
"/usr/lib/ruby…]

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [September 14, 2010, 4:50pm UTC](https://www.ruby-forum.com/t/getting-a-unique-filename/194960/4 "2010-09-14T16:50:19Z")

</div>

On Sep 14, 6:15 am, Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> OUTPUT\_FORMAT(elf64-x86-64)  
> /usr/lib/libc.so: invalid ELF header (LoadError)  
> from /usr/lib/ruby/1.8/rake/rake\_test\_loader.rb:5:in `load' from /usr/lib/ruby/1.8/rake/rake_test_loader.rb:5 from /usr/lib/ruby/1.8/rake/rake_test_loader.rb:5:in `each’  
> from /usr/lib/ruby/1.8/rake/rake\_test\_loader.rb:5  
> rake aborted!  
> Command failed with status (1): [/usr/bin/ruby1.8 -w -I"lib"  
> "/usr/lib/ruby…]

Oops. I’ve had the fix in git for a while now, just forgot to push it  
out.

I’ve just pushed out 1.1.3. Try that.

Regards,

Dan
