File5:49 with Jason Seifer
The File class handles file input and output in Ruby. When working with files in a programming language, there are different things you need to tell the computer to do in order to assure that files are correctly handled on the disk. There are different modes that files can be opened as well: reading, writing, and different combinations of both of those.
The file class handles file input and output in Ruby. 0:00 When working with files in a programming language, 0:05 there are different things that you need to tell the computer to do. 0:08 In order to assure that files are correctly handled on the disk. 0:11 There are different modes that files can be opened in, as well. 0:16 Things like open for reading, writing, and different combinations of both of those. 0:20 The file class takes care of these low level concepts for you, and 0:26 let's you work with files in a way that is more like programming conventional Ruby. 0:30 Let's take a look at that now, using Workspaces. 0:35 Now here's the file documentation. 0:39 And if we read about it, it says that file is an abstraction of 0:41 any file object accessible by the program. 0:45 Now that really doesn't tell us much, 0:49 but what we're interested in doing is working with files. 0:51 So if we look at the methods on the left side of the page here and 0:56 head on down to the open method, we can give it a file name and a mode. 1:02 And then we get a file block. 1:08 Now, that is quite a bit to take in 1:12 especially when we think about the modes of files. 1:15 And the modes are documented in the IO class, and you'll find a link 1:18 to that in the documentation right below this video as well. 1:23 So, here are the different modes that we have for reading and writing files, 1:26 read-only, read-write, write-only. 1:31 And write only with the appending option. 1:36 So let's go ahead and just see how this works. 1:39 I'm gonna create a new file here, and call it file.rb. 1:43 Now, we have to use the open method, and 1:48 we do that by calling File, the class, and open. 1:51 Now let's just give this an example file. 1:56 We'll call it example.txt. 1:58 And now we need to give it a mode. 1:59 And I'm just going to give this write only mode right now. 2:03 And then we get a block that we can access this file inside of, So 2:06 now that we have it, let's go ahead and put some code in here. 2:12 And this is gonna be very simple. 2:16 We're just going to have somebody enter their name. 2:17 And then we're gonna write that to the file. 2:20 So we can say enter your name. 2:24 And then we'll get that from standard input and cut off the new line character. 2:29 And then we'll do the same thing for email. 2:35 So now what we can do with these 2:44 is use some of the same methods that we're used to when printing to the screen. 2:47 We can also print to a file. 2:52 So we could say file. 2:53 .puts and we'll just write a string to it. 2:55 Name and the name that was just entered. 3:00 And we'll do the same thing with email. 3:05 Okay. 3:12 So now I'm going to run this and let's see what happens. 3:13 Enter your name. 3:18 Jason. 3:20 Enter your email. 3:21 email@example.com. 3:22 Okay. 3:27 And the program ended. 3:27 Now what we need to do is refresh this file listing. 3:30 So if we hit Refresh, we can see there is now an example.txt file. 3:33 So if we open that up, you can see that it has my name and email address. 3:40 I'm gonna clear my screen and run this one more time. 3:46 Enter your name, and this time I'll enter Nick, and firstname.lastname@example.org. 3:48 Now, if I go ahead and open up this example.txt file. 3:55 We can see it's been overwritten. 3:59 And it has Nick and email@example.com. 4:01 It does not have the info that I placed in it earlier. 4:04 Now why is that? 4:07 That has to do with the file mode that we opened it in. 4:09 So we entered it in Write Only mode, and it says it truncates the existing file. 4:13 If we wanted to append things to it, we'd have to use this A+ mode. 4:19 Which will append data to the end of the file. 4:24 So let's go ahead and go back and change that from w to a+. 4:28 I'm gonna close the example text file. 4:33 Gonna run this again, my name is Jason, here's my e-mail address. 4:41 Okay, program ended. 4:47 Now if we open up the example txt file, 4:48 we can see this as functioning how we expect it to. 4:51 The file is being appended to rather than rewritten. 4:54 Now there are plenty of other methods to use when working with files. 4:58 We can find out the base name of a file. 5:04 And that is going to be the last component of the file name. 5:08 So if we look at the base name of this path right here, 5:13 it's just this last part of ruby.rb rather than the whole path. 5:17 Now there are tons of things we can do. 5:23 With files, we can find out when a file was created, or the birth time. 5:26 It will give us back a time object. 5:32 We can change the ownership of a file. 5:35 We can find a path to the file, and the size of the file in bytes. 5:38 As well. 5:43 Try working with files on your own now, using work spaces. 5:45
You need to sign up for Treehouse in order to download course files.Sign up