[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Any nice way to get a temporary directory?

Kenneth McDonald

10/23/2008 8:24:00 PM

I'd like to be able to obtain a temporary directory in the same way I
can easily obtain a temporary file, but there doesn't seem to be a
function for this. Currently, I'm creating a temp file, getting it's
name, unlinking it, and then creating a dir with the same name. But
this is ugly, and in theory subject to a race condition. Is there a
better way of doing this?

Thanks,
Ken

10 Answers

Craig Demyanovich

10/23/2008 8:42:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Could you call Dir.mkdir with the result of Dir.tmpdir plus the name of the
directory that you want to create?

Regards,
Craig

Kenneth McDonald

10/23/2008 8:45:00 PM

0

There is, sadly, no Dir.tmpdir function in Ruby.

Ken


On Oct 23, 2008, at 3:42 PM, Craig Demyanovich wrote:

> Could you call Dir.mkdir with the result of Dir.tmpdir plus the name
> of the
> directory that you want to create?
>
> Regards,
> Craig


Avdi Grimm

10/23/2008 8:55:00 PM

0

On Thu, Oct 23, 2008 at 4:45 PM, Kenneth McDonald
<kenneth.m.mcdonald@sbcglobal.net> wrote:
> There is, sadly, no Dir.tmpdir function in Ruby.

There is, actually. You have to require 'tmpdir' and then you'll have it.


--
Avdi

Home: http:...
Developer Blog: http:.../devblog/
Twitter: http://twitte...
Journal: http://avdi.livej...

Kenneth McDonald

10/23/2008 9:49:00 PM

0

Ah, thank you. Sorry for my previous mistake.

Ken

On Oct 23, 2008, at 3:55 PM, Avdi Grimm wrote:

> On Thu, Oct 23, 2008 at 4:45 PM, Kenneth McDonald
> <kenneth.m.mcdonald@sbcglobal.net> wrote:
>> There is, sadly, no Dir.tmpdir function in Ruby.
>
> There is, actually. You have to require 'tmpdir' and then you'll
> have it.
>
>
> --
> Avdi
>
> Home: http:...
> Developer Blog: http:.../devblog/
> Twitter: http://twitte...
> Journal: http://avdi.livej...
>


Avdi Grimm

10/23/2008 9:51:00 PM

0

On Thu, Oct 23, 2008 at 5:49 PM, Kenneth McDonald
<kenneth.m.mcdonald@sbcglobal.net> wrote:
> Ah, thank you. Sorry for my previous mistake.

No problem. Considering there is almost no documentation for the
'tmpdir' library, and nothing to hint that requiring a special library
will magically make new methods appear on an existing class, this is
definitely one of Ruby's better-kept secrets :-)



--
Avdi

Home: http:...
Developer Blog: http:.../devblog/
Twitter: http://twitte...
Journal: http://avdi.livej...

ara.t.howard

10/24/2008 12:59:00 AM

0


On Oct 23, 2008, at 2:24 PM, Kenneth McDonald wrote:

> I'd like to be able to obtain a temporary directory in the same way
> I can easily obtain a temporary file, but there doesn't seem to be a
> function for this. Currently, I'm creating a temp file, getting it's
> name, unlinking it, and then creating a dir with the same name. But
> this is ugly, and in theory subject to a race condition. Is there a
> better way of doing this?
>
> Thanks,
> Ken
>



this keeps backwards compact, but also lets you do

Dir.tmpdir do

puts "in tmpdir #{ Dir.pwd }"

end


the tmpdir will be created for you and cleaned up too





class Dir
module Tmpdir
require 'tmpdir'
require 'socket'
require 'fileutils'

unless defined?(Super)
Super = Dir.send(:method, :tmpdir)
class << Dir
remove_method :tmpdir
end
end

class Error < ::StandardError; end

Hostname = Socket.gethostname || 'localhost'
Pid = Process.pid
Ppid = Process.ppid

def tmpdir *args, &block
options = Hash === args.last ? args.pop : {}

dirname = Super.call(*args)

return dirname unless block

turd = options['turd'] || options[:turd]

basename = [
Hostname,
Ppid,
Pid,
Thread.current.object_id.abs,
rand
].join('-')

pathname = File.join dirname, basename

made = false

42.times do
begin
FileUtils.mkdir_p pathname
break(made = true)
rescue Object
sleep rand
:retry
end
end

raise Error, "failed to make tmpdir in #{ dirname.inspect }"
unless made

begin
return Dir.chdir(pathname, &block)
ensure
unless turd
FileUtils.rm_rf(pathname) if made
end
end
end
end

extend Tmpdir
end




a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Nobuyoshi Nakada

10/24/2008 2:29:00 AM

0

Hi,

At Fri, 24 Oct 2008 05:24:20 +0900,
Kenneth McDonald wrote in [ruby-talk:318458]:
> I'd like to be able to obtain a temporary directory in the same way I
> can easily obtain a temporary file, but there doesn't seem to be a
> function for this. Currently, I'm creating a temp file, getting it's
> name, unlinking it, and then creating a dir with the same name. But
> this is ugly, and in theory subject to a race condition. Is there a
> better way of doing this?

Dir.mktmpdir is available in lib/tmpdir.rb since 1.8.7.

--
Nobu Nakada

Bitmaster

12/20/2010 7:26:00 PM

0


Thanks Scott and Steve for your replys.

Scott, I did confirm that the part number was the same for Mystic too.
If that is the case do you still need the measurement? Please let me
know and I will do that tonight.

Thanks again,
Gary


--
Bitmaster
This USENET post sent from http://rgpa...

seymour-shabow

12/20/2010 8:04:00 PM

0

Bitmaster wrote:
> Thanks Scott and Steve for your replys.
>
> Scott, I did confirm that the part number was the same for Mystic too.
> If that is the case do you still need the measurement? Please let me
> know and I will do that tonight.
>
> Thanks again,
> Gary
>
>

No, I will look for it based on that. I have a mystic PF accessible,
and I have the spare I got, so I should be able to pull the spare one or
if I did already (likely as I de-populated the rest of the PF) I can
compare to make sure it's the right one.

Will let you know.

scott

Bitmaster

12/21/2010 3:25:00 PM

0


Steve was able to find the part I needed and sent to me. Thanks again
to both Steve and Scott for responding. Very much appreciated.


--
Bitmaster
This USENET post sent from http://rgpa...