Ruby 有 mkdir -p 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11463343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:17:27 来源:igfitidea点击:
Does Ruby have mkdir -p?
提问by ma11hew28
Possible Duplicate:
How to create directories recursively in ruby?
可能的重复:
如何在 ruby 中递归创建目录?
In Ruby, how could I do:
在 Ruby 中,我该怎么做:
mkdir -p cool/beans
Here's what I came up with:
Dir.mkdir('cool') unless File.directory?('cool') cool_beans_path = File.join('cool', 'beans') Dir.mkdir(cool_beans_path) unless File.directory?(cool_beans_path)But, isn't there a better way?
I know I could do:
system('mkdir', '-p', File.join('cool', 'beans'))But, that's not platform independent, is it? Like, it works on Mac but not on Windows, right?
这是我想出的:
Dir.mkdir('cool') unless File.directory?('cool') cool_beans_path = File.join('cool', 'beans') Dir.mkdir(cool_beans_path) unless File.directory?(cool_beans_path)但是,难道没有更好的方法吗?
我知道我可以这样做:
system('mkdir', '-p', File.join('cool', 'beans'))但是,这不是独立于平台的,是吗?就像,它适用于 Mac,但不适用于 Windows,对吗?
回答by Max
require 'fileutils'
FileUtils.mkdir_p 'cool/beans'

