Chapter 6. Working with Files

In many ways, this chapter is closely related to the previous chapter, “Working with Pipes”. The principals are the same because both are implemented as children of the IO class.

Indeed, in Unix, the distinction between IO devices and “files” is significantly blurred. For instance, /dev/random is a “file” in the sense that it has a name and path. But it’s also a direct way to “read” the random numbers generator in to any program that accepts files as input.

In Ruby, all IO objects have basic input and output support, depending on which file mode the object was opened with.

Opening and Closing Files

Certainly, one of the tried-and-true conventions in Ruby is using code blocks to ensure that resources are automatically ...

Get Ruby Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.