Locked lesson.
About this lesson
We'll learn to write and append data to a file.
Exercise files
Download this lesson’s related exercise files.
Write to a File - Solution.docx59.3 KB Write to a File.docx
59 KB
Quick reference
Write to a File
Let's write to a file!
When to use
Writing to a file is easy, you just need to determing whether you want to write to the beginning or end of the file.
Instructions
To write to a file, choose the appropriate open mode so that you can write to the file at either the beginning or end of the file.
Writing to the beginning, use r
Writing to the end, use a
Want to erase the old contents first, use w
To add something to a file, just puts it to the file variable:
my_file = File.open("some_file.txt", "r")
my_file.puts "Some Text to add"
Hints & tips
- Write to the beginning, r
- Write to the end, a
- 00:04 Okay, in the last video, I threw a bunch of open file modes at you and
- 00:07 it might have been a little overwhelming.
- 00:10 In this video, we're going to walk through a few of them and
- 00:12 actually give you some examples and show you how to write two files.
- 00:15 So I'm just going to create a comment here =begin and
- 00:18 come down here, and =end, just to comment out all this stuff for now.
- 00:23 So we talked about the different modes, that's r, w and
- 00:27 a with their corresponding pluses on them.
- 00:30 So let's say we want to write something to a file and
- 00:33 we've got our stuff.txt file, and it's just got this list of names.
- 00:38 Let's say we want to add Tina to our file and we want to put her at the end.
- 00:42 The a is right only and a+ is read and write.
- 00:47 So we can do a, because we're not going to do anything else to it.
- 00:50 I've commented this stuff out.
- 00:52 So write only.
- 00:54 And with a, the file pointer starts at the end of the file, right?
- 00:58 That's why we're using a, as opposed to r+, which is read and write.
- 01:02 We wouldn't want to use w, because w overrides the entire file if it exists and
- 01:06 we don't want to overwrite that.
- 01:08 We just want to add something, I think of a as sort of like append or
- 01:11 appending something.
- 01:12 So to do this, it's pretty simple.
- 01:14 We just name our variable, my_file and
- 01:16 then we're going to put something in that file.
- 01:19 So let's use the puts method and we can do it by just adding .puts,
- 01:23 because puts is a method and we can string any method onto anything else.
- 01:27 That's sort of the nature of object oriented programming.
- 01:29 So now, we can just type in Tina.
- 01:31 That's the thing we want to puts.
- 01:33 And if we save this, come down here and run it.
- 01:35 Nothing happened down here.
- 01:36 But if we come up to our stuff.txt and then open it, now we see boom,
- 01:40 Tina appears at the bottom.
- 01:42 So that's pretty cool, it worked.
- 01:43 I'm going to delete her and save this file.
- 01:45 Now, let's say we wanted to add her to the beginning of the file.
- 01:49 Well, we could do r+ which is read, write mode and
- 01:52 the file pointer is at the beginning of the file.
- 01:55 So everything else stays the same, my_file.puts "Tina".
- 01:59 So if we save this and run it, nothing happened there.
- 02:02 But if we open our file, boom, Tina is at the top of the list.
- 02:05 So that is pretty cool.
- 02:06 It's very simple just my myfile.puts, whatever you want to put in there and
- 02:10 that's why these modes are important.
- 02:11 If you want it at the front or top of the file or
- 02:14 plus if you want it at the bottom, a.
- 02:16 We've got a little bit of time left.
- 02:18 Let's undo this and I just want to show you this a by itself is
- 02:23 right only mode, and the thing will go at the end.
- 02:27 But here, we're doing other stuff.
- 02:29 We're trying to read it also.
- 02:31 So what happens if we try and use a.
- 02:33 So if we run this thing, boom, we get an error not opened for reading.
- 02:37 A is only right only not reading and writing.
- 02:40 So that's another reason why these modes are important.
- 02:43 If you pick the wrong one, you're going to get an error.
- 02:44 If you want to do more than just write to it, if you also want to read,
- 02:48 you need to use a mode that allows for reading and writing.
- 02:51 So r, w and a most of the time, we're going to use our to read.
- 02:55 r+ to wright at the beginning of the file, a+ to wright at the end of the file.
- 03:01 w, I tend just not to use w, because either you're writing only or you're reading and
- 03:05 writing.
- 03:06 But if you use w or w+, it's going to overwrite everything in your file.
- 03:10 It's going to erase everything in your file and
- 03:13 you really don't often want to do that.
- 03:14 If you wanted to do that, you could create a whole new file or something.
- 03:17 But anyway, those are the modes, those are how to open a file, read a file,
- 03:21 do things to a file.
- 03:23 Very, very simple with Ruby.
- 03:24 In the next video, we're going to look at gems and
- 03:26 how to add third party functionality to your program which is very, very cool.
- 03:30 So that's all for this video.
Lesson notes are only available for subscribers.