3/21/2008

The way to make directory with Ruby

If I make a directory with ruby.

  1. #!/usr/bin/ruby
  2. begin
  3. directory_name = Dir::pwd + "/" + "test"
  4. if FileTest::directory?(directory_name)
  5. return
  6. end
  7. Dir::mkdir(directory_name)
  8. end


Above codes allow
me to create a directory.

1 line expresses that this file is ruby process.

2 line is Main thread starting sign, 8 line is end sign.

4 line checks to exists or not.

7 line real make directory.


6 comments:

Clay Teeter said...

This is great but leads to a race condition. I would recommend wrapping the mkdir in a try/catch block

begin
Dir::mkdir(directory_name)
rescue
puts "the directory already exists"
end

Ryan C. Moon said...

I know this is an older post, but I have been using:
Dir::mkdir(directory) unless File.exists?(directory)

Tass said...

I used something very similar to what you're doing, Ryan C. Moon.

Dave said...

Yea. the Dir::mkdir comment works like a champ

Anonymous said...

Your mode of telling the whole thing in this piece of
writing is really good, every one be capable of effortlessly understand it, Thanks a lot.


Also visit my homepage - updated blog post

Anonymous said...

It can take pictures in a burst mode of up to nine times better
than on the fleshlight original iPad.
You might decide to take the most advantage of
this career choice, contacting insurance companies is absolutely essential.

If you've recently purchased a fleshlight adult toy or are thinking about it, or stray away from that sort of activity.