Project

env_var

0.0
No commit activity in last 3 years
No release in over 3 years
Check if an environment variable is set to an enabled or disabled value. Fetch an environment variable as an Array of Strings or Symbols.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.16
~> 10.0
~> 3.0
 Project Readme

EnvVar

Build Status

Check if an environment variable is set to an enabled or disabled value. Fetch an environment variable as an Array of Strings or Symbols.

Usage

enabled?/disabled?

Set an environment variable:

export VARIABLE_NAME=1     # enabled
export VARIABLE_NAME=true  # enabled
export VARIABLE_NAME=0     # disabled
export VARIABLE_NAME=false # disabled

And check if it's enabled or disabled:

require "$ENV"

enable_foo if $ENV.enabled?("VARIABLE_NAME")
disable_foo if $ENV.disabled?("VARIABLE_NAME")

Note that if the environment variable is not set enabled? and disabled? can both be false. If something is not enabled that does not mean it was explicitly disabled.

Don't do this:

if !$ENV.enabled?("VARIABLE_NAME")
if !$ENV.disabled?("VARIABLE_NAME")

Do this:

if $ENV.disabled?("VARIABLE_NAME")
if $ENV.enabled?("VARIABLE_NAME")

$ENV::W

Return an environment variable's value as an Array of words (Strings):

export VARIABLE_NAME="a,b,c"
export ANOTHER_VARIABLE=

Then in Ruby:

$ENV::W["VARIABLE_NAME"]     # ["a", "b", "c"]
$ENV::W["ANOTHER_VARIABLE"]  # []

# Or
$ENV.W("VARIABLE_NAME")      # ["a", "b", "c"]
$ENV.w("VARIABLE_NAME")      # ["a", "b", "c"]

Mnemonic %w(a b c).

$ENV::I

Return an environment variable's value as an Array of Symbols:

$ENV::I["VARIABLE_NAME"]  # [:a, :b, :c]

# Or
$ENV.I("VARIABLE_NAME")   # [:a, :b, :c]
$ENV.i("VARIABLE_NAME")   # [:a, :b, :c]

Mnemonic %i(a b c).

Otherwise, Use it Like You Would ENV

p $ENV["VARIABLE_NAME"]
p $ENV.delete("VARIABLE_NAME")
$ENV.each { |name, value| ... }

Without $ENV

require "env_var"

enable_foo if EnvVar.enabled?("VARIABLE_NAME")
disable_foo if EnvVar.disabled?("VARIABLE_NAME")
p EnvVar["VARIABLE_NAME"]
p EnvVar.delete("VARIABLE_NAME")
EnvVar.each { |name, value| ... }

Monkey Patch

require "env_var/env"

enable_foo if ENV.enabled?("VARIABLE_NAME")
disable_bar if ENV.disabled?("VARIABLE_NAME")
p ENV::i["VARIABLE_NAME"]
p ENV::w["VARIABLE_NAME"]

# ENV::W, ENV::I do not work

Why?

Got tired of writing this over and over in various applications:

enable_foo if %w[true 1 on].include?(ENV["ENABLE_FOO"])

See Also

  • Envied - Ensures presence and type of your app's ENV-variables

Author

Skye Shaw (skye DOT shaw AT gmail )

License

The gem is available as open source under the terms of the MIT License.