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 16. Reading and Writing Files

If you did the Study Drills from the last exercise you should have seen all sorts of commands (methods/functions) you can give to files. Here’s the list of commands I want you to remember:

close – Closes the file. Like File->Save.. in your editor.

read – Reads the contents of the file. You can assign the result to a variable.

readline – Reads just one line of a text file.

truncate – Empties the file. Watch out if you care about the file.

write('stuff') – Writes “stuff” to the file.

For now, these are the important commands you need to know. Some of them take parameters, but we do not really care about that. You only need to remember that write takes a parameter of a string you want to write 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