Does Ruby have mkdir -p? [duplicate]

本小妞迷上赌 提交于 2019-12-03 03:27:45

问题


Possible Duplicate:
How to create directories recursively in ruby?

In Ruby, how could I do:

mkdir -p cool/beans
  1. 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?

  2. 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?


回答1:


require 'fileutils'
FileUtils.mkdir_p 'cool/beans'


来源:https://stackoverflow.com/questions/11463343/does-ruby-have-mkdir-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!