On Tue, 31 Jan 2006, Logan Capaldo wrote:

>
> On Jan 30, 2006, at 12:09 PM, charlie bowman wrote:
>
>> irb(main):002:0> Dir.mkdir(".timeclock")
>> won't the above faile once the directory is created.  I need to run a
>> check each time the application is ran.
>
> Yes, that's why you wrap it in an if (or unless) statement.
>
> unless File.exist?(".timeclock")
>   Dir.mkdir(".timeclock")
> end
> # Optionally make sure .timeclock is  a directory
> unless File.directory?(".timeclock")
>   puts ".timeclock is a not a directory."
>   exit( 1 )
> end
> # do whatever.

one cannot both check and/or create a dir in an atomic fashion.  in fact, one
cannot even create a dir in an atomic fashion on some filesystems.  about the
best you can do is

   class Dir
     def self::create d
       begin
         mkdir d # always try to create it
       rescue Errno::EEXIST
         nil # ignore failure to do so
       end
       new d  # this will throw an error if it does not exist!
     end
   end

regards.

-a
-- 
sleep is the best meditation.  -- h.h. the 14th dali lama