Mike Griffin

About | Archives | |

Using Pipe With Ruby Scripts

I’ve been writing some Ruby scripts recently to parse some text files and I was looking for a way to pass in only part of a file instead of the full 1500 lines. If I was bash scripting, I could use head to extract part of the text file and then pipe it in to my script. But that wasn’t going to work in this case because I was passing the filename as a command line argument like this:

input = File.open(ARGV[0])

A quick search and I found this blog post describing exactly what I wanted to achieve.

Instead of using File.open(ARGV[0]), I could use ARGF.read and it would work in both scenarios. Now I can run the Ruby script in two ways:

./script.rb filename


head filename | ./script.rb

Very handy.