Skip to main content

How to Read an Environment Variable in Ruby

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Settings that change between computers, such as a database address or an API key, belong in environment variables. Ruby reads them from ENV, and ENV.fetch('NAME', default) gives you the value, or the default when the variable isn't set.

Syntax​

Syntax
<value> = ENV.fetch('<NAME>', <default>)
<value> = ENV['<NAME>']
PartWhat it does
ENV.fetch('<NAME>', <default>)The variable's value, or <default> when it isn't set.
ENV.fetch('<NAME>')Without a default, raises KeyError when the variable isn't set.
ENV['<NAME>']The value, or nil when it isn't set.

The Ruby documentation for ENV.fetch: "If name is the name of an environment variable, returns its value". Otherwise, "if a default value is given (but not a block), returns the default value".

The Example​

The example reads GREETING with an empty-string default and prints it with inspect, which shows the quotes, first with the variable set and then without it.

Prerequisites​

  • Ruby. This guide uses Ruby 4.0.10.

Step 1: Write the Code​

Create a file named main.rb:

main.rb, which reads ENV.fetch('GREETING', '') into greeting and puts it with inspect.

main.rb
greeting = ENV.fetch('GREETING', '')

puts "GREETING is: #{greeting.inspect}"

Step 2: Set the Variable and Run It​

Set GREETING to Hello and run the script. Then remove the variable and run it again:

Terminal
export GREETING=Hello
ruby main.rb
unset GREETING
ruby main.rb

A zsh terminal where export GREETING=Hello and ruby main.rb print GREETING is: "Hello", and after unset GREETING the same command prints GREETING is: "".

Output
GREETING is: "Hello"
GREETING is: ""

With the variable set, fetch returned "Hello"; without it, the default "".

fetch, fetch with a Default, or []?​

  • ENV.fetch('NAME') for settings the program can't run without: a missing variable raised key not found: "GREETING_NOT_SET" (KeyError), which stops the script at once.
  • ENV.fetch('NAME', default) when a fallback makes sense.
  • ENV['NAME'] when nil is a fine answer: it returned nil for a missing variable.

Common Mistakes​

MistakeWhat happensDo this instead
Using ENV['NAME'] for a required settingnil flows on and fails later, far from the causeUse ENV.fetch('NAME')
Expecting a numberEvery value is a String, such as "3000"Convert with Integer(...)
Setting the variable in one terminal and running Ruby in anotherThe default, or nil: each terminal has its own variablesSet it in the terminal that runs the script

Troubleshooting​

SymptomLikely causeFix
key not found: "…" (KeyError)fetch without a default met a missing variableSet the variable, or pass a default
The value is always the defaultThe variable isn't set in this terminalSet it as in Step 2

FAQ​

Are variable names case-sensitive?​

On macOS and Linux, yes. Windows treats GREETING and greeting as the same variable.

Can I change a variable from Ruby?​

Yes: ENV['NAME'] = 'value' sets it for this script and the programs it starts.

Conclusion​

ENV.fetch('NAME', default) reads an environment variable with a fallback; without the default it raises KeyError, which suits required settings. ENV['NAME'] returns nil when the variable is missing.

Additional Resources​

  • ENV, in the Ruby documentation.