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.

  1. What does this program print? mode = ENV.fetch("APP_MODE", "development") puts mode

    Answer

    development

    fetch returns the second argument when the variable is missing, which keeps a development machine working.

  2. What does this program print? begin ENV.fetch("NO_SUCH_SETTING_FOR_THE_QUIZ") rescue KeyError => e puts e.class end

    Answer

    KeyError

    Without a fallback, fetch raises KeyError when the name is not in the environment.

  3. What does this program print when it is run with no arguments? puts ARGV.length puts ARGV.first.inspect

    Answer

    0 / nil

    No arguments means an empty array, and first on an empty array is nil, which inspect prints as nil.

  4. What does ruby show.rb one two print for this program? puts ARGV.length puts ARGV.first

    Answer

    2 / one

    Each word is one element of ARGV, so the length is two and the first element is the first word.

  5. 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 options

    Answer

    {verbose: false}

    The default hash is printed unchanged when no flag is passed on the command line.

  6. What does ruby flags.rb --verbose print 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.

  7. What is the exit status of this program? warn "Need a file name" exit 1

    Answer

    1

    exit 1 stops the program with status 1, which a calling script reads as a failure.

  8. Where does warn write its message?

    Answer

    To standard error

    A warning belongs on standard error, which keeps it apart from the data the program prints.

  9. 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.

  10. 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