Reading from ARGF as a Stream

The simplest way to use ARGF is by treating it as a single stream of text and looping over each line in that stream.

Like other streams in Ruby, ARGF responds to each_line. We’ve seen this before, both when working with standard input and when working with files. In the former case, we called each_line on $stdin, and in the latter, we called each_line on a file object. In this case, we call ARGF.each_line, and just like in those other examples the block that we pass to ARGF.each will be invoked once per line in the stream.

With ARGF, the contents of that stream either are the same as $stdin or are a continuous reading of all the files passed on the command line.

Here’s an example, then, of the simplest possible ...

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.