- [Instructor] In this chapter, we're going to learn how to work with directories, and we're going to begin by learning how to create and delete them. Directories, by their nature, are much simpler than files. They don't contain any text content. They're just a repository for holding different files. There a couple of ways that we can create directories. The first, is by using the Dir class, that's built into Ruby. Dir being short for directory. The Dir class has a class method called mkdir, short for make directory and you give it a file path and it makes a directory at that path, with the name that you give it, it's that easy.
You'll remember that we've also seen that there's something built into the Ruby standard library called FileUtils. And we can require that in our code and then make use of it. FileUtils also has a method called mkdir, works exactly the same way, it's just on a different class, and it's really up to you which one you use. If you're in the process of using file utilities, it's probably more convenient to use its version, but if you haven't included those file utilities, there's no reason to load them in just to call that method. You can use the one that's built on the Dir class.
Once we've created our directory, then we can put files into it. Now when we're done with the directory, and we want to delete it, there are a couple of ways we can do that as well. The directory class has a delete class method, and again, we just provide a file path to that directory, and it will delete it, it's that simple. Except, if the directory contains files or other directories, it will see it as not empty and it will not delete it. You can call dir.empty? on the file path to find out if it's empty or not. And it's a good idea to try and wrap a delete in a check to see whether the directory's empty.
If it's not empty, then it would be up to you to go in and remove all the files and directories that are in there or you can hold on and I'll show you a shortcut to get there. Another option of course, is using that FileUtils that we just saw before, but this time, it doesn't have the same name, it's not called delete. It's called rmdir, that's a more parallel construction to mkdir, so with FileUtils you would use the rmdir method and give it a file path to the file you want to delete. It works the same way, and it doesn't delete the directory if it's not empty.
Now, if you did want to delete a directory that still has files or other directories inside of it, there is a method that'll let you do that. In FileUtils, it's called rm_r. The second r stands for recursive, and what this does is it deletes the directory and all its contents recursively. No matter how deep they go, it just keeps deleting files and folders, whatever levels might be below this particular directory that you're trying to delete. If you need to get rid of a directory and all of its contents, this is the method you want to use.
Let's try it out, let's go into irb and let's create a new directory in our Ruby sandbox and then we can work on deleting it. So I'll go into irb and let's start by setting a path here. Let's do current_dir is going to be equal to file.dirname for the current file that I'm working with now. So that just gives me a dot, that's the current directory, that's alright. New_dir is going to be equal to file.join the current dir with a new directory we're going to call new.
So that's the path for the directory that I want to create. It's okay that it's a relative path. I could also get an absolute path, it doesn't make a difference. Dir.mkdir will take either one. New_dir alright and it created it. Now let's come over her and let's take a look, and sure enough, there's our new directory. It's empty, there's nothing inside of it, but it created it, just like we wanted. Dir.empty? New_dir. Yes, it is empty. Now let's do dir.delete new_dir. Now, each time I pass in this variable new_dir, remember it's equal to this string that's up here.
I'm just telling it the path to get to that file. Come up here and you'll see that it's gone. It's that easy. Let's take a look at how we do it with file utilities. So we'll start out by requiring FileUtils from the Ruby standard library, and then we'll do fileutils.mkdir new_dir. So it created it for us, we come back over here, we see once again the file is there and now let's add a file to it. Let's do filepath equals and we're going to create a new file using file.join to get the new path.
It's going to be new_dir and our file is going to be called delete_me.txt, this'll just give us a path to that file. And then we'll say file.open, we've learned how to do that with our filepath using right mode do file and inside that file we'll put file.puts this is a test file. and end. Okay, so we should have a new file in there now, there is is, delete_me. So now let's try checking this directory again to see is it empty, new_dir.
No, it's not empty now. Let's try just deleting it. We could try it with dir.delete first. New_dir and you'll see we get a e not empty error, directory not empty. Let's try it with file utilities. FileUtils.rmdir with new_dir, nope, same thing, get the same kind of error. Now let's try it with that other one which is FileUtils.rm_r new_dir and now it deleted it, including the file.
So overall, creating and deleting directories is pretty simple, as long as you remember that most of the time, the directory needs to be empty before it can be deleted, unless you use that special method that's in the file utilities.
- File system basics
- Writing to and reading from files
- Renaming, deleting, and copying files
- Creating and editing directories
- Working with common data formats
- ERB templating