Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Ruby have mkdir -p? [duplicate]

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?

like image 393
ma11hew28 Avatar asked Jul 13 '12 02:07

ma11hew28


People also ask

How to create a new directory in Ruby?

Using Ruby's Mkdir Method To Create A New Directory If you want to create a new folder with Ruby you can use the Dir. mkdir method. If given a relative path, this directory is created under the current path ( Dir. pwd ).

How to check if a directory exists in Ruby?

If it matters whether the file you're looking for is a directory and not just a file, you could use File. directory? or Dir. exist? . This will return true only if the file exists and is a directory.

What is directory in Ruby?

A directory is a location where files can be stored. For Ruby, the Dir class and the FileUtils module manages directories and the File class handles the files.

How do I change directory in Ruby?

chdir : To change the current working directory, chdir method is used. In this method, you can simply pass the path to the directory where you want to move. The string parameter used in the chdir method is the absolute or relative path.


1 Answers

require 'fileutils' FileUtils.mkdir_p 'cool/beans' 
like image 163
Max Avatar answered Sep 29 '22 13:09

Max