Ruby: Command Line Arguments with ARGV
Background
In the same way that we pass arguments to methods in Ruby, we can also pass arguments to whole programs. That’s exactly what command line arguments do. Each time we run our Ruby program we can feed in different command line arguments, and get different results.
Intro to the Command Line
Discover the power of this simple yet essential text-based tool and increase your productivity as a developer.Try it for freeWhy Learn It?
Using command line arguments adds a new tool to your Ruby toolbelt, which you can use to get user input into your program if you choose. The gets
and gets.chomp
methods can be used for user input.
Using Command Line Arguments
Ruby captures command line arguments with a special array named ARGV
. When written inside your Ruby program, ARGV
will take take a command line command that looks like this:
ruby testing_argv.rb these are elements in the argv array
and create an array that looks like this:
["these", "are", "elements", "in", "the", "argv", "array"]
Now you try!
Trying out ARGV
- In Sublime Text, create a file named
testing_argv.rb
by going to File > New File … - Once you’ve created your test file, make sure to save it in an easy-to-find location, and let’s start writing our code!
- Set up
ARGV
to capture command line arguments Insidetesting_argv.rb
, assignARGV
to an array calledinput_array
, which will bind the contents ofARGV
to a variable.
Since ARGV
creates an array, you can call any method on input_array
that you can call on a normal array. For instance, puts input_array.length
will return with the number of arguments a user passed.
You can also print the results of the ARGV
array using puts input_array.to_s
. The full testing_argv.rb
file might look something like this:
To test your file, run the program on the command line with multiple arguments following the filename, like this: ruby testing_argv.rb these are elements in the argv array
Your command line should tell you there are 7 items in the array, and print them out to your console, like this:
["these", "are", "elements", "in", "the", "argv", "array"]
Using Splat to separate the first argument from the rest
For your program, you may want to use the first command line argument as a command, and then use the rest of the arguments as a string of text that you can do things with. Here’s a new Ruby expression that can help you accomplish this simply:
first_arg, *the_rest = ARGV
Here, you’re assigning two variables at the same time! The comma separates them, and the *
is called a splat. Because of the *
, Ruby knows to assign ARGV[0]
to first_arg
, and the rest of the arguments to the_rest. Let’s prove it:
In your program, write:
puts first_arg
puts the_rest
Then run ruby testing_argv.rb first and all the rest!
inside your command line.
How can you use this Technique?
The command line can be a powerful interface for a program you’ve written. These methods will enable you to capture user input, and then put it to good use.
For instance, if you built and ATM program with Ruby, you could use command line arguments to call methods and modify your bank account. You could write an ARGV array that takes your first command line argument to perform a method, like “withdraw”, and then use the arguments after it, perhaps “$20”, to specify how much money to withdraw.
There are infinite ways to use ARGV and splats to get data from the command line and make it do something for you. If you want to get to the bottom of how ARGV works, check out its Ruby-doc here.
Author
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Meet the full teamRelated articles
Learn more on Codecademy
- Free course
Intro to the Command Line
Discover the power of this simple yet essential text-based tool and increase your productivity as a developer.Beginner Friendly1 hour - Course
Learn the Command Line
Learn about the command line, starting with navigating and manipulating the file system, and ending with redirection and configuring the environment.With CertificateBeginner Friendly4 hours - Free course
Learn the Command Line: Configuring the Environment
Learn how to configure your environment and set up your settings and preferences from the command line.Beginner Friendly1 hour