Locked lesson.
About this lesson
How to open another file in your Ruby file.
Quick reference
Opening a File
Opening a file with Ruby is super easy!
When to use
Use this any time you have an external file that you need to access.
Instructions
To open a file in Ruby, use the File class and .new method:
my_file = File.new("filename.txt")
When you open a file, you need to close it when you're done.
my_file.close
To pull stuff out of your file line by line:
while ! my_file.eof?
stuff = my_file.gets.chomp
puts stuff
end
Hints & tips
- my_file = File.new("filename.txt")
- my_file.close
- 00:04 In this video, I want to talk about opening a file.
- 00:06 So we're done with all the class stuff.
- 00:08 So let's just go ahead and delete all this good stuff.
- 00:12 So files, let's talk about opening files.
- 00:13 So here on the left, we have our hello.rb files just one file, but
- 00:17 there are times when you may want to open other files.
- 00:20 But if you have other files and you want to access them,
- 00:23 get information out of them.
- 00:24 A lot of times,
- 00:25 you'll access a database,but sometimes you just want to open a file.
- 00:29 So that's what we're going to talk about it in this video.
- 00:31 So I'm going to come over here.
- 00:32 I'm going to right-click on this little folder guy here and
- 00:35 I'm just going to create a new file, and let's just call it stuff.txt.
- 00:39 It's just a simple text file and I'm going to open it, and
- 00:43 let's just say this is a customer list.
- 00:46 And then let's go John, Tim, Bob, Mary, Bluto.
- 00:51 So if we save this and close it, just going to double-click to make sure.
- 00:56 Okay, so it saved.
- 00:57 Now let's say we've got a program, a Ruby program and
- 00:59 we want to access the stuff that's in this file.
- 01:02 Well, how do we do that?
- 01:03 Well, it's pretty easy.
- 01:03 With Ruby, we're going to use the file class and an open method.
- 01:07 It's like a lot of things in Ruby.
- 01:08 We start out just by naming a variable and I'm going to call this my_file.
- 01:12 And now, we're going to call the file class is just capital F-I-L-E.
- 01:16 Remember, all of our classes start with a capital letter.
- 01:19 So there is a good chance that this is a class and
- 01:22 then .open is the open method for the file class.
- 01:25 We're going to pass a parameter in and the parameter is just the name of the file.
- 01:30 So it's stuff.txt.
- 01:32 So this file is in the same directory as our stuff.txt in our hello.rb.
- 01:38 They're all in the same directory.
- 01:38 So we can just call stuff.txt.
- 01:40 If this was in a different directory,
- 01:42 we would have to type in the name of the directory or whatever.
- 01:45 Since it's in the same directory, that's all we have to do.
- 01:48 So that's it.
- 01:49 So let's save this.
- 01:50 And now if we run this program, nothing is going to happen,
- 01:53 because we haven't set anything.
- 01:54 But let's go puts my_file and then let's say, inspect and
- 01:59 see if we can see what's going on in here.
- 02:02 It's pulled it stuff.txt and it doesn't really help us out.
- 02:05 It doesn't really tell us a whole lot.
- 02:07 So now, what do we want to do?
- 02:08 Well, I should say, we've opened this file and
- 02:11 we've stuffed the contents into this my_file variable.
- 02:14 We also need to close the file.
- 02:15 So let's go my_file.close and
- 02:17 I don't think you actually have to do this, but it's a good idea.
- 02:22 If you open a file, you should explicitly close it.
- 02:24 So we'll just put this down in.
- 02:26 But now, how do we actually access this stuff that's in there?
- 02:29 If we just puts this out, if we save this,
- 02:31 we get this address in memory where it sits, but it still doesn't help us out.
- 02:35 Well, we know our stuff.txt has stuff in it.
- 02:38 So let's create a while loop that loops through that text and
- 02:42 prints it out on the screen line by line.
- 02:44 So it's pretty easy to use our while loop and I'm going to use an exclamation point,
- 02:49 which we remember means not.
- 02:50 And then we go my_file.eof? and EOF stands for end of file.
- 02:58 So as long as it's not at the end of the file and a question mark is asking,
- 03:01 isn't at the end of the file?
- 03:03 As long as it's not, do stuff, right?
- 03:06 So what do we want to do?
- 03:07 Well, let's create a new variable called,
- 03:11 let's call it stuff and we'll set that equal to my_file.get.
- 03:16 So we want to gets the stuff from that file and let's chomp it,
- 03:19 because we always chomp our stuff and then let's just puts that stuff.
- 03:23 So now if we save this, come down here and run it again.
- 03:27 Boom, we get our customer list, and all the items.
- 03:30 And what's going on here is while it's not at the end of the file,
- 03:33 it's going to go line by line and
- 03:35 just puts out to the screen each thing on that line if we change this to print.
- 03:39 Change the formatting a bit.
- 03:41 Put it all on one line, but that's basically what's going on.
- 03:44 So then at the end, we close our file and everything works good.
- 03:47 So that's how you open files and read stuff.
- 03:51 You could do this a number of ways.
- 03:53 You don't have to use a while loop.
- 03:54 You can use any other sort of loop, but just that easy, opening files.
- 03:58 In the next video, we'll talk about stuffing the file contents into an array.
Lesson notes are only available for subscribers.