Does Ruby have mkdir -p?

RubyFilePathDirectoryMkdir

Ruby Problem Overview


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

Ruby Solutions


Solution 1 - Ruby

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionma11hew28View Question on Stackoverflow
Solution 1 - RubyMaxView Answer on Stackoverflow