Chapter 1Reading from Files

Our first concern when processing text is to get the text into our program, and perhaps the most common place to source text is from the humble file. Whether it’s log files from a server, exports from database, or text you’ve written yourself, there’s lots of information that lives on the filesystem. Learning to read from files effectively opens up a world of text to process.

Throughout the course of this chapter, we’ll look at how we can use Ruby to reach text that resides in files. We’ll look at the basics you might expect, with some methods to straightforwardly read files in one go. We’ll then look at a technique that will allow us to read even the biggest files in a memory-efficient way, by treating files as ...

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.