Forum: Ruby Question about tempfile.rb - Ruby vs C

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (Guest)
on 2007-04-24 21:15
(Received via mailing list)
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));
}
4d5b5dd4e263d780a5dfe7ac8b8ac98c?d=identicon&s=25 Tim Pease (Guest)
on 2007-04-24 21:43
(Received via mailing list)
On 4/24/07, Daniel Berger <djberg96@gmail.com> 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
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (Guest)
on 2007-04-24 22:55
(Received via mailing list)
On 4/24/07, Tim Pease <tim.pease@gmail.com> 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
This topic is locked and can not be replied to.