Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save priyankamk/d8a56c87a7303fe31a71db194193f739 to your computer and use it in GitHub Desktop.
Save priyankamk/d8a56c87a7303fe31a71db194193f739 to your computer and use it in GitHub Desktop.
ARGV - ruby command line arguments

Ruby command line arguments By Alvin Alexander. Last updated: June 3 2016 Ruby FAQ: How do I read command line arguments in a Ruby script (Ruby command line args)?

To read command line args in a Ruby script, use the special Ruby array ARGV to get the information you need. Here are a few examples.

  1. Getting the number of command line args To get the number of command line arguments passed in to your Ruby script, check ARGV.length, like this:

quit unless our script gets two command line arguments

unless ARGV.length == 2 puts "Dude, not the right number of arguments." puts "Usage: ruby MyScript.rb InputFile.csv SortedOutputFile.csv\n" exit end In the case of this script, I am expecting two command line arguments, and if I don't get two args, I exit the script.

  1. Getting the value of the Ruby command line args To work with the actual values of the arguments that are passed into your Ruby program, just treat ARGV as a normal Ruby array, and get the values like this:

our input file should be the first command line arg

input_file = ARGV[0]

our output file should be the second command line arg

output_file = ARGV[1]

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