January 2024
Intermediate to advanced
718 pages
20h 15m
English
When you run a Ruby program from the command line, you can pass in arguments. These are accessible from your Ruby code in two different ways.
First, the global array ARGV contains each of the arguments passed to the running program. Create a file called cmd_line.rb that contains the following:
| | puts "You gave #{ARGV.size} arguments" |
| | p ARGV |
When we run it with arguments, we can see that they get passed in:
| | $ ruby cmd_line.rb ant bee cat dog |
| | You gave 4 arguments |
| | ["ant", "bee", "cat", "dog"] |
Often, the arguments to a program are the names of files that you want to process. In this case, you can use a second technique: the variable ARGF is a special kind of I/O object that acts like ...
Read now
Unlock full access