O'Reilly logo

Learn Ruby the Hard Way: A Simple and Idiomatic Introduction to the Imaginative World of Computational Thinking with Code, Third Edition by Zed A. Shaw

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exercise 15. Reading Files

You know how to get input from a user with gets.chomp or ARGV. Now you will learn about reading from a file. You may have to play with this exercise the most to understand what’s going on, so do the exercise carefully and remember to do your checks. Working with files is an easy way to erase your work if you are not careful.

This exercise involves writing two files. One is the usual ex15.rb file that you will run, but the other is named ex15_sample.txt. This second file isn’t a script, but a plain text file we’ll be reading in our script. Here are the contents of that file:

This is stuff I typed into a file. It is really cool stuff. Lots and lots of fun to have in here.

What we want to ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required