Processing Command-Line Arguments to Your Code
Just as you can pass arguments to methods in your Ruby code, you can pass arguments from the command line to the Ruby script itself. Ruby provides mechanisms for capturing arguments passed to the script and allowing you to read and parse them as part of your script.
ARGV
Any command-line arguments after the program filename are available to your Ruby program in the global array ARGV. For instance, assume test.rb contains the following program:
| ARGV.each { |arg| p arg } |
If you invoke it with the following command line:
| $ ruby -w test.rb "Hello World" a1 1.6180 |
It’ll generate the following output:
| "Hello World" |
| "a1" |
| "1.6180" |
There’s a gotcha here for all you C programmers. In Ruby, ...
Get Programming Ruby 3.3 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.