How to Work With Directories in Ruby

Welcome to our guide on working with directories in Ruby! In this tutorial, we’ll cover all the basics you need to get started using the mkdir method and FileUtils module to create and manipulate directories in your code.

Table of Contents

Using Ruby’s Mkdir Method to Create a New Directory

To create a new directory in Ruby, you can use the mkdir method. Here’s an example of how to use it:

Dir.mkdir("new_directory")

This will create a new directory called “new_directory” in the current working directory.

You can also create a new directory with a different path by specifying it as an argument:

Dir.mkdir("/path/to/new_directory")

You Can Get a Few Errors

There are a few errors you might encounter when using the mkdir method. For example, you might get a “File exists” error if you try to create a directory that already exists.

To avoid this error, you can use the exist? method to check if the directory exists before trying to create it:

if !Dir.exist?("new_directory")
  Dir.mkdir("new_directory")
end

Another error you might encounter is a “Permission denied” error if you don’t have the necessary permissions to create a directory. In this case, you can either try running your code with higher permissions or specifying a different directory where you do have permission to create a new directory.

Two Solutions

There are two ways you can handle these errors in your code. The first is to use begin and rescue blocks to catch the errors and handle them gracefully:

begin
  Dir.mkdir("new_directory")
rescue Errno::EEXIST
  puts "Directory already exists"
rescue Errno::EACCES
  puts "Permission denied"
end

The second solution is to use the mkdir method’s optional mode and options arguments to specify how the directory should be created:

Dir.mkdir("new_directory", 0700, mode: :existing_only)

The mode argument specifies the permissions for the new directory, and the :existing_only option tells Ruby to only create the directory if it doesn’t already exist.

Advanced Operations with the FileUtils Module

The Ruby FileUtils module provides a number of advanced directory manipulation methods. Some of the most useful ones include:

  • cp_r: Copies a directory and all its contents to a new location
  • mv: Renames a directory
  • rm_r: Deletes a directory and all its contents

Here’s an example of how to use the cp_r method to copy a directory:

FileUtils.cp_r("/path/to/original", "/path/to/copy")

How to Rename Directories

To rename a directory, you can use the mv method from the FileUtils module:

FileUtils.mv("/path/to/old_directory", "/path/to/new_directory")

This will rename the “old_directory” to “new_directory”.

How to Change Your Current Directory

To change your current directory in Ruby, you can use the Dir.chdir method:

Dir.chdir("/path/to/new_directory")

This will change the current working directory to the specified path.

Listing Files & Directories with Pattern Matching

You can use the Dir class to list the files and directories in a given directory. Here’s an example of how to do it:

files = Dir.entries("/path/to/directory")
puts files

This will list all the files and directories in the specified directory.

You can also use pattern matching to filter the results. For example, to list only directories that start with “a”, you can do the following:

directories = Dir.glob("/path/to/directory/a*")
puts directories

Summary

In this tutorial, we learned how to work with directories in Ruby using the mkdir method and FileUtils module. We covered how to create, rename, and delete directories, as well as how to change the current working directory and list files and directories with pattern matching. We also discussed how to handle common errors and use advanced options like permissions and existing-only creation.

We hope you found this guide helpful and are now ready to start working with directories in your own Ruby projects. Happy coding!

How to Work With Directories in Ruby
Scroll to top