Skip to content

Instantly share code, notes, and snippets.

@mitchellh
Created May 18, 2011 06:02
Show Gist options
  • Save mitchellh/978055 to your computer and use it in GitHub Desktop.
Save mitchellh/978055 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# This is a really stupid Ruby implementation of `cat` as
# an example of detecting '-' as stdin/stdout.
#
# Usage: rubycat.rb infile outfile
raise "Specify an input file!" if !ARGV[0]
raise "Specify an output file!" if !ARGV[1]
# Here we detect if either of the files are '-' and if so,
# we use the proper stdin/stdout stream.
infile = ARGV[0] == '-' ? STDIN : File.open(ARGV[0])
outfile = ARGV[1] == '-' ? STDOUT : File.open(ARGV[1], "w")
# Naive load the input stream into memory then write it out
outfile.write(infile.read)
@sirupsen
Copy link

I'm disappointed you're not using unless instead of if !! :)

@mitchellh
Copy link
Author

I'm not a big fan of unless, I actually tend to avoid it everywhere since my brain more readily parses if statements. Just my personal taste :)

@sirupsen
Copy link

Fair enough, I thought it was odd when I just came to Ruby -- but at some point it just started feeling natural to use and read. I guess a lot of newcomers hate us for using it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment