[ruby] Create Directory if it doesn't exist with Ruby

I am trying to create a directory with the following code:

Dir.mkdir("/Users/Luigi/Desktop/Survey_Final/Archived/Survey/test")
    unless File.exists?("/Users/Luigi/Desktop/Survey_Final/Archived/Survey/test")  

However, I'm receiving this error:

No such file or directory - /Users/Luigi/Desktop/Survey_Final/Archived/Survey/test (Errno::ENOENT)

Why is this directory not being created by the Dir.mkdir statement above?

This question is related to ruby

The answer is


Simple way:

directory_name = "name"
Dir.mkdir(directory_name) unless File.exists?(directory_name)

How about just Dir.mkdir('dir') rescue nil ?


Another simple way:

Dir.mkdir('tmp/excel') unless Dir.exist?('tmp/excel')