4 comments
Very useful and easy to understand! thanks
Thanks for reading! 🙂
I found this piece very helpful. Thank you.
Thanks for your feedback! 🙂
Did you know that you can navigate your file system with Ruby?
With the Ruby “Dir” class.
You can list directory entries, change your current directory & even create new folders!
Here’s an example:
filenames = Dir.entries(".")
This entries
method returns an array of filename entries. Every entry is a string, so you’ll need to combine this with the Ruby File class if you want to actually read these files.
Btw, this dot (.
) represents the current directory.
This will be the directory your code is running from, NOT the directory you’re (in your terminal) when running the code.
Because of that…
It can be helpful to check your current directory by using the Dir.pwd
method.
Now:
Let’s learn how to use the Dir
class to create new directories (mkdir
), rename them (mv
), and find file names that follow a specific pattern (glob
).
If you want to create a new folder with Ruby you can use the Dir.mkdir
method.
Example:
Dir.mkdir("testing")
If given a relative path, this directory is created under the current path (Dir.pwd
).
You can get a few errors:
Errno::EEXIST
)Errno::EACCES
)Errno::ENOENT
)The last error usually happens when you’re trying to create nested directories.
Or if you’re using an absolute path that doesn’t exist.
Two solutions:
Dir.exists?
)Let’s keep learning!
If you need an extra bit of horsepower you can bring in the FileUtils
module from the standard library.
It includes methods like mkdir_p
that create nested directories in one step.
Here’s an example:
require 'fileutils' FileUtils.mkdir_p("/tmp/testing/a/b")
Pretty cool, right?
That’s not all, FileUtils
also brings extra options for all file operations in the form of keyword arguments. Like the verbose
option (prints Linux commands) & the noop
(don’t change files) option.
Give it a try!
Ruby allows you to do every operation you can do from your Operating System user interface, or from a terminal.
For example…
You can rename a directory like this:
FileUtils.mv("/tmp/a", "/tmp/b")
You’ll also need to use FileUtils
here because mv
is not available on the Dir
class.
Because all directory operations are run from the current directory, you may want to change it.
You can use the Dir.chdir
method to do this.
Example:
Dir.chdir("/tmp") { Dir.entries(".") }
This works in two ways:
Notice that chdir
only works within your Ruby process, it doesn’t affect the “outside world”.
In other words…
It won’t change your shell’s working directory after your Ruby program stops running.
Want to find all Ruby files in a folder? An easy task with the glob
method!
Example:
Dir.glob("*.rb")
You can use any other extension you want, like “.txt”, or “.yml”. Or any other text that’s part of the file name.
Want to find files inside all folders?
Yep, it’s possible:
Dir.glob("**/*.rb")
The result is an array with all the file names, including the relative path. You can remove the path & get only the file name by using the File.basename
method on every file of the list that you get from glob
.
You have learned how to work with directories in Ruby using the Dir
& FileUtils
classes.
Now it’s your turn to put this into practice.
Thanks for reading! 🙂
Very useful and easy to understand! thanks
Thanks for reading! 🙂
I found this piece very helpful. Thank you.
Thanks for your feedback! 🙂