O'Reilly logo

Text Processing with Ruby by Rob Miller

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required