Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

env_var

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

env_var

  • 0.0.2
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

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 %w(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.

FAQs

Package last updated on 22 Jun 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc