Created
May 18, 2011 06:02
-
-
Save mitchellh/978055 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
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 :)
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
I'm disappointed you're not using
unless
instead ofif !
! :)