Environment and CLI
ENV variables
Programs read their configuration from ENV, which maps process environment variables by name.
Fetch with a fallback keeps development simple while production passes the real value.
mode = ENV.fetch("APP_MODE", "development") puts mode
APP_MODE=production ruby app.rb
production
ARGV
Command line arguments arrive in ARGV as an array of strings.
Length counts the arguments and first reads the leading one.
puts ARGV.length puts ARGV.first
ruby args.rb one two
2
one
OptionParser
OptionParser turns flags such as verbose into an options hash with help text built in.
The on call registers each flag, and parse! consumes the flags from ARGV.
require "optparse" options = { verbose: false } OptionParser.new do |parser| parser.on("-v", "--verbose", "Run verbosely") do options[:verbose] = true end end.parse! p options
ruby flags.rb --verbose
{verbose: true}
Exit codes
A nonzero exit status signals failure to the shell and to calling scripts.
Warn writes the complaint to standard error, and exit 1 stops the program with status 1.
if ARGV.empty? warn("Need a file name") exit(1) end puts "Got #{ARGV.first}"
ruby check.rb
Need a file name
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does this program print?
mode = ENV.fetch("APP_MODE", "development") puts modeAnswer
development
fetch returns the second argument when the variable is missing, which keeps a development machine working.
-
What does this program print?
begin ENV.fetch("NO_SUCH_SETTING_FOR_THE_QUIZ") rescue KeyError => e puts e.class endAnswer
KeyError
Without a fallback, fetch raises KeyError when the name is not in the environment.
-
What does this program print when it is run with no arguments?
puts ARGV.length puts ARGV.first.inspectAnswer
0 / nil
No arguments means an empty array, and first on an empty array is nil, which inspect prints as nil.
-
What does
ruby show.rb one twoprint for this program?puts ARGV.length puts ARGV.firstAnswer
2 / one
Each word is one element of ARGV, so the length is two and the first element is the first word.
-
What does this program print when it is run with no arguments?
require "optparse" options = { verbose: false } OptionParser.new do |parser| parser.on("-v", "--verbose", "Run verbosely") do options[:verbose] = true end end.parse! p optionsAnswer
{verbose: false}
The default hash is printed unchanged when no flag is passed on the command line.
-
What does
ruby flags.rb --verboseprint for the OptionParser program?Answer
{verbose: true}
The on block runs for the matching flag and sets the value, and parse! consumes the flag from ARGV.
-
What is the exit status of this program?
warn "Need a file name" exit 1Answer
1
exit 1 stops the program with status 1, which a calling script reads as a failure.
-
Where does
warnwrite its message?Answer
To standard error
A warning belongs on standard error, which keeps it apart from the data the program prints.
-
What does
ENV["APP_MODE"]do when the variable is not set?Answer
Returns nil
The bracket form returns nil, which is why the lesson uses fetch with a fallback.
-
What type does every element of ARGV have?
Answer
A String
The shell passes text, so a number argument has to be converted before arithmetic.
Article author: Arthur Isaev