Wrapping Up

That’s about it for reading files. We looked at how to open a file and what we can do with the resulting File object. We covered reading files in one go and processing them like streams, and why you’d prefer one or the other. We explored how we can use the methods offered by Enumerable to transform and manipulate the content of files. We looked at line-by-line processing and reading arbitrary numbers of bytes, and how we can seek to arbitrary locations in the file to replicate some of the functionality of a database.

With these techniques, we’ve gained an impressive arsenal for reading text from files large and small. Next, we’ll take our newfound knowledge of streams and apply it to another source of text: standard input.

Footnotes ...

Get Text Processing with Ruby 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.