- [Instructor] In this movie, we will look at the basics of working with the file system. I assume that you know the most basic facts. There're files on your hard drive and they're grouped into different directories, or you might call them folders, or we're going to use the word directories. And we can navigate through those directories in order to find files. We can also use Ruby to navigate the file system as well to find files and directories, to create or delete them, and to read and write to our files. To do that, we're going to need to tell Ruby how to find the file or directory by using the path as a string.
So for example, we could have a file called examplefile.dot.txt, which is inside a directory called Ruby_project, which is inside a directory called documents. This would give us a string that would define the path to get to that file so that Ruby would know how to locate it because we don't have that graphical user interface to allow us to click and find the file. We have to do with a string. Now if you're on Unix, this is going to look very familiar to you, if you're more familiar with Windows, you may be familiar seeing file paths that looks something like this.
Where we have a C colon at the front to tell it that we're on our C Drive, and then we use backslashes as separators between each of those directories and the file. So as you can see, different operating systems handle the description of these file paths differently. Unix, Linux and the Mac operating system are going to use the forward slash as a file path separator. While Windows users that backslash instead. This difference goes way back to the early days when windows was based on dos. If you've worked on Windows, you've probably used the backslash many times. You may also know that the forward slash works on Windows in many, many cases.
That's been true for a long time, Windows would let you use them interchangeably, but there's still some cases when only the backslash will do. So which one of these do we use in our Ruby code? We want our Ruby code be cross platform and be able to run on any operating system that has Ruby installed. You can use the forward slash most of the time and it will work on all platforms, even on Windows. You don't want to ever use the backslash because it's unlikely to work on a Unix based machine. So the forward slash is the better choice of the two. However, Ruby gives us an even better way to handle it.
The file class has a class method called join. Join takes a series of strings and joins them together with the appropriate file separator for the system that Ruby is currently working with. We don't even have to think about it, Ruby will take care of it for us. The file doesn't even have to exist. Join is literally just joining those strings together with the right separator. The file doesn't even have to exist and Ruby doesn't check. After all, we might be defining a file path for a new file that we haven't even created yet.
Let's go to irb and try out join. Here I'm in the command line and you can see that in Unix I typed pwd and it gave me the path to the current directory that I'm in. I'm currently in my documents directory. It doesn't matter which directory you're in I just want you to see that there is a path that's there. If you're on Windows you can just type a cd and it should also return the same information to you, pwd will work some versions of Windows as well. Alright, so let's go into irb and let's just try out file join. So we just do file join, it's a class method so we don't need an instance of it.
And then we can put in anything we want. This, is, a, comma, test. And you can see that it joins them together with an appropriate file separator, put test.txt, right? It's not actually checking to see that the file is there, it's just taking those strings and putting them together with the correct separator. Now you may be on Windows and you may have gotten forward slashes just now. That's okay don't worry about it. File join looks at your underlying operating system and figures out which is the appropriate choice.
So, whether you got a forward slash or backslash in an example doesn't make a difference. Just know that Ruby is going to handle it for you so that you won't run into a problem. So why you can use forward slashes most of the time, file join is the best practice.
- 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