Ruby ARGF For Command Line Applications

April 11, 2017

Ruby ARGF For Command Line Applications

About a year ago, I discovered the Ruby ARGF class as a way to help you read files in ruby scripts.

Ruby logo on railroad tracks

From the Ruby documentation:

ARGF is a stream designed for use in scripts that process files given as command-line arguments or passed in via STDIN.

Concatenate file contents

One nifty trick I picked up from this thoughtbot post is that besides concatenating of file contents, you have to shift flags off the ARGV array if you want a Ruby CLI application that accepts options.

To get a better handle on this, let’s look at an example program.

Example CLI – Summing Numbers

#!/usr/bin/env ruby

##argf_demo.rb script
require 'pry'
require 'optparse' do |opts|
  opts.banner = "\nARGF Demo app\n"

  opts.on("-h", "--help", "Help:") do |h|
    puts opts

  opts.on("-v", "--version") do |v|
    puts opts.banner
    puts "Version 0.0.1"


ARGF.each do |line|
  break if line=="quit\n"
  sum = line.strip.split(" ").map(&:to_i).inject { |s, n| n + s }
  puts "Sum: #{sum}"

For now, let’s focus on ARGF. Here you run the program with:

$ ruby argf_demo.rb numbers1.txt numbers2.txt

To give you an idea of the numbers in each file, here they are:


2 2
4 5


3 2
3 3

If you run the program, you should see output like:

Sum: 4
Sum: 9
Sum: 5

You would see 6 if I didn’t have the quit word in the numbers2.txt file.

Shifting ARGS off ARGV with OptionParser

The cool thing about OptionParser is that it takes care of shifting arguments off ARGV for you. So you can enter the following at the terminal:

$ ruby argf_demo.rb -v numbers1.txt numbers2.txt

And you will see the following output:

ARGF Demo app
Version 0.0.1
Sum: 4
Sum: 9
Sum: 5


If you’re ever in need of an easy way to read in files from the command line, ARGF could be a handy tool for you. Here is the demo repo I used for this blog post.