January 2024
Intermediate to advanced
718 pages
20h 15m
English
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.
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, ...
Read now
Unlock full access