From: Robert Klemme Date: 2010-03-05T00:24:01+09:00 Subject: Re: A gem for handling temporary file(s)? 2010/3/4 Albert Schlef : > Robert Klemme wrote: >> On 03/03/2010 08:35 PM, Caleb Clausen wrote: >>> one with enough information to make that judgment. >>> >> Albert still did not disclose what the external program should do with >> the temporary file [...] > > Thank you all, this task is behind me already. Considering your last comment about files not being deleted I'd say it's not done yet. :-) > If it interests you, then the external command I'm using is 'dot' (from > the graphviz package). > > The command I'm using is something like this: > >  dot "%s" -Tgif -o "%s" -Tsvg -o "%s" > > (I need three temporary files, in this example. 'dot' reads the first > file and writes images to the next two files.) > > Smart guys would say that instead, on POSIX systems, I could write the > command as... > >  dot /dev/fd/10 -Tgif -o /dev/fd/11" -Tsvg -o /dev/fd/12 > > ...but then I'd need to read simultaneously from fd/11 and fd/12 because > I'm not sure which is written first. But I need my program to work on > Windows too and I'm not sure it supports this "sophistication". Yeah, probably not a good idea to go into that direction. Btw, do you need to read those files while they are written or is it sufficient to wait for process termination (e.e. use system) before you access them? > Caleb Clausen wrote: >> I'm not expert enough to be certain about this, but by doing this >> you'll be creating a tempfile race condition [...] > > I don't care much about robustness because the command is run from an > interactive application by a single user and I'm using the files > immediately. There isn't much a chance for a race condition. > > Caleb Clausen wrote: >> I _think_ that if you create a temporary file with Tempfile, and then >> DON'T CLOSE IT, you can safely pass the tempfile's name to an external >> command. > > For some reason, the files aren't always deleted (well, it seems they're > never deleted). I don't know why. At first I suspected 'dot' re-creates > the files, but when I do "ls -i" I see that the inodes haven't changed. > But I can live with this bug. Maybe I could add some code to the > "destructor" of my class to explicitly delete these files. Does Ruby > support "destructors"? We have begin ... ensure ... end The usual way to do this is to write a method. See http://blog.rubybestpractices.com/posts/rklemme/002_Writing_Block_Methods.html Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/