Ruby

Ruby

Made by DeepSource

Usage of ENV[] that fails silently RB-W1021

Bug risk
Minor
Autofix

ENV[] silently fails and returns nil when the environment variable is unset, which may cause unexpected behaviors when the developer forgets to set it. On the other hand, ENV.fetch raises KeyError or returns the explicitly specified default value.

Bad practice

ENV['DB_URL']
db_url = ENV['DB_URL']

Recommended

ENV.fetch('DB_URL')
db_url = ENV.fetch('DB_URL')

# This is allowed as nil is falsey
!ENV['X']
# Calling some_method wouldn't fail silently if ENV['X'] is nil
ENV['X'].some_method