Learn Ruby the Hard Way – Exercise 15 Reading Files

Here is my work for Exercise 15.

First I made a .txt file and typed the sentences in the exercise. Then, I made a Ruby file named ex15 and typed the code.

Exercise 15 code
Exercise 15 code
Exercise 15 output
Exercise 15 output

Study Drills
1. Above each line, write out in English what that line does.

Commented Exercise 15 Code
Commented Exercise 15 code

2. If you are not sure ask someone for help or search online. Many times searching for “ruby THING” will find answers to what that THING does in Ruby. Try searching for “ruby open.”
I read about Ruby open and read. The open method takes a filename parameter, and returns a file object. The read method on a file object returns nil or a string. In our example, it returns a string containing the sentences we typed in our .txt file!

3. I used the word “commands” here, but commands are also called “functions” and “methods.” You will learn about functions and methods later in the book.
Okay! Looking forward to it.

4. Get rid of the lines 8-13 where you use gets.chomp and run the script again.

The content of the file is printed.
The content of the file is printed.

5. Use only gets.chomp and try the script that way. Why would one way of getting the filename be better than another?
I’m not sure about this one. I guess if you get the filename as a parameter for the ARGV, you can just run the script and the file will be opened. If you use gets.chomp, you have to type input to open the file. I’ll have to come back to this question later.

6. Start irb to start the Ruby shell, and use open from the prompt just like in this program. Notice how you can open files and run read on them from within irb?

Opening the file from the Ruby shell.
Opening the file from the Ruby shell.

First I had an error because I was not entering the filename as a string. The open method requires a string for the filename parameter.

7. Have your script also call close() on the txt and txt_again variables. It’s important to close files when you are done with them.

File objects are closed.
File objects are closed.





6 thoughts on “Learn Ruby the Hard Way – Exercise 15 Reading Files

  1. Hi Stacey. I’ve been trying o learn ruby as a fresher and find your blog very helpful. I wish i had found it few months earlier 🙂 Your explanations are thorough and easy to understand Thank you very much for your enthusiasm and hard work you share with the likes of me.

    Liked by 1 person

  2. Thanks a lot for going through these exercises thoroughly stacey – I only found your site the other day and I can tell it’s going to be very helpful for me.


    Liked by 1 person

  3. Hi Stacy, I’m a little late to the party, but I just found your blog. I’m currently learning ruby and it’s great to compare my notes and progress with others.

    If you can recall, do you know where you learnt the close commands from? As this got me really stuck in this part of the exercise. I’m almost positive the previous exercises didn’t teach me the close command, then in this one he just mentions to close them. Ok, but how?! I learnt from your blog!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s