简体   繁体   中英

How to create nested directory in ruby

Suppost I want to create a directory like "a/b/c" under the current dir.

How to make it?

I have tried this:

Dir.mkdir("a/b/c").

However it does not work.

I guess this is because parent directories of c do not exist. If you try to do the same in the shell you will also get an error. You can force creation of parent directories if they do not exist with the -p option:

mkdir -p a/b/c

which in Ruby can be done with mkdir_p found in the FileUtils module:

require "fileutils"

FileUtils::mkdir_p "a/b/c"

您需要使用FileUtils.mkdir_p

什么能让你的生活更轻松

system 'mkdir "/home/awais/development/rails/testing2"'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM