Manipulating ARGV

If we pass arguments to our Ruby script on the command line, then the ARGV array will contain those arguments as values. In this case, as we’ve seen, ARGF will assume that these command-line arguments refer to files, so it will try to read from them.

So far, all we’ve done with this knowledge is to pass files on the command line. But ARGV isn’t a fixed proposition. It’s something that you can modify, adding or removing elements to either pretend something was passed on the command line or pretend that it wasn’t.

This comes in useful in two different scenarios. First, when the actual arguments passed on the command line contain things that aren’t files, which you likely want ARGF to ignore rather than attempt to read. And second, ...

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.