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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.