Question about tempfile.rb - Ruby vs C

D

Daniel Berger

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

v_args[0] = INT2FIX(fileno(tmpfile()));

rb_call_super(1, v_args);
}

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));
}
 
T

Tim Pease

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?

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
 
D

Daniel Berger

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top