# Question about tempfile.rb - Ruby vs C

**URL:** <https://www.ruby-forum.com/t/question-about-tempfile-rb-ruby-vs-c/92838>\
**Category:** Ruby\
**Created:** [April 24, 2007, 9:15pm UTC](https://www.ruby-forum.com/t/question-about-tempfile-rb-ruby-vs-c/92838 "2007-04-24T21:15:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [April 24, 2007, 9:15pm UTC](https://www.ruby-forum.com/t/question-about-tempfile-rb-ruby-vs-c/92838/1 "2007-04-24T21:15:52Z")

</div>

Hi all,

Just curious - why was Ruby’s temporary file handling class  
(tempfile.rb) written in pure Ruby instead of using the tmpfile()  
function? That function appears to be supported on most platforms,  
including MS Windows.

Are there file locking or threading issues I’m not aware of?

The tmpfile() function does have the advantage of deleting itself when  
all references to it are gone, instead of waiting until the Ruby  
process itself dies. Perhaps there’s no real advantage to that,  
though?

Like I said, not an issue, just curious.

Regards,

Dan

PS - Below is a simple version I created for Solaris (before I  
realized how ubiquitous the tmpfile function was):

#include \<ruby.h\>  
#include \<stdio.h\>

#define VERSION “0.0.1”

VALUE mSolaris, cTempfile;

/\* :call-seq:

- Solaris::Tmpfile.new =\> file

- 
- Creates a new, anonymous temporary file in your Tempfile::TMPDIR  
directory,

- or /tmp if that cannot be accessed. If your $TMPDIR environment  
variable is

- set, it will be used instead. If $TMPDIR is not writable by the  
process, it

- will resort back to Tempfile::TMPDIR or /tmp.  
\*/  
static VALUE tempfile\_init(VALUE self){  
VALUE v\_args[1];

void Init\_tempfile(){  
mSolaris = rb\_define\_module(“Solaris”);  
cTempfile = rb\_define\_class\_under(mSolaris, “Tempfile”, rb\_cFile);

rb\_define\_method(cTempfile, “initialize”, tempfile\_init, 0);

rb\_define\_const(cTempfile, “TMPDIR”, rb\_str\_new2(P\_tmpdir));  
rb\_define\_const(cTempfile, “VERSION”, rb\_str\_new2(VERSION));  
}

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [April 24, 2007, 9:43pm UTC](https://www.ruby-forum.com/t/question-about-tempfile-rb-ruby-vs-c/92838/2 "2007-04-24T21:43:55Z")

</div>

On 4/24/07, Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> all references to it are gone, instead of waiting until the Ruby  
> process itself dies. Perhaps there’s no real advantage to that,  
> though?

The Ruby Tempfile class creates a finalizer that will delete the temp  
file when it is collected by the garbage collector; the Ruby Tempfile  
also deletes the temp file when it is closed. Your paragraph above  
makes it sound like this is not the case.

> Like I said, not an issue, just curious.

I do not know why the C tmpfile function was not used. Maybe Matz  
likes programming in Ruby more than programming in C 😉

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [April 24, 2007, 10:55pm UTC](https://www.ruby-forum.com/t/question-about-tempfile-rb-ruby-vs-c/92838/3 "2007-04-24T22:55:01Z")

</div>

On 4/24/07, Tim P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > The tmpfile() function does have the advantage of deleting itself when  
> > all references to it are gone, instead of waiting until the Ruby  
> > process itself dies. Perhaps there’s no real advantage to that,  
> > though?
> 
> The Ruby Tempfile class creates a finalizer that will delete the temp  
> file when it is collected by the garbage collector; the Ruby Tempfile  
> also deletes the temp file when it is closed. Your paragraph above  
> makes it sound like this is not the case.

I was basing my comment on the Pickaxe 2, p. 741 which says,  
“…temporary files are automatically deleted when the Ruby process  
terminates”.

Regards,

Dan
