Skip to content

Temporarily set environment variable values by treating the environment as a stack, and using blocks to implicitly control scope

License

Notifications You must be signed in to change notification settings

eventide-project/env-var

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EnvVar

Temporarily set environment variable values by treating the environment as a stack, and using blocks to control scope

Example

ENV["SomeEnvVar"] = "some original value"

EnvVar.push("SomeEnvVar", "some new value") do
  # The SomeEnvVar environment value is
  # "some new value" inside this block
  p ENV["SomeEnvVar"]
  # => "some new value"
end

# The SomeEnvVar environment value is restored
# to the original value at the end of the block
p ENV["SomeEnvVar"]
# => "some original value"

Pushing a Change to an Environment Variable

push(variable_name, value, &action)
EnvVar.push("SomeEnvVar", "some new value") do
  # The SomeEnvVar environment value is
  # "some new value" inside this block
end

The value of the environment variable will be changed to the new value before the block executes, and will be restored to the original value after the block executes.

If the environment variable does not already exist, it will be declared in the environment and then unset at the conclusion of the action block.

Returns

A dictionary of the environment variable name and the environment variable's original value. The dictionary's type is Hash.

Parameters

Name Description Type
variable_name The name of the environment variable whose value will be affected String
value The value to assign to the environment variable String
action The block to be executed in the context of the new environment variable value Proc

Pushing a Change to a Set of Environment Variables

push(hash, value, &action)
new_values = {
  "SomeEnvVar" => "some new value",
  "SomeOtherEnvVar" => "some other new value",
}

EnvVar.push(new_values) do
  # The SomeEnvVar environment value is
  # "some new value" inside this block
  # The SomeOtherEnvVar environment value is
  # "some other new value" inside this block
end

The value of an environment variable will be changed to the new value before the block executes, and will be restored to the original value after the block executes.

If an environment variable does not already exist, it will be declared in the environment and then unset at the conclusion of the action block.

Returns

A dictionary of the environment variable names and the environment variables' original values. The dictionary's type is Hash.

Parameters

Name Description Type
hash The dictionary of environment variable names whose values will be affected, and the new values Hash
action The block to be executed in the context of the new environment variable values Proc

Getting an Environment Variable's Value

get(variable_name)
ENV["SomeEnvVar"] = "some value"
EnvVar.get("SomeEnvVar")
# => "some value"

Returns

The environment variable's value. The environment variable's value is String. If the environment variable is not set, the returned value is nil.

Parameters

Name Description Type
variable_name The name of the environment variable whose value will be retrieved String

Fetching an Environment Variable's Value

fetch(variable_name)
ENV["SomeEnvVar"] = "some value"
EnvVar.fetch("SomeEnvVar")
# => "some value"

If the environment variable is not set, KeyError is raised.

EnvVar.fetch("SomeUnsetVar")
# => key not found: "SomeUnsetVar" (KeyError)

Returns

The environment variable's value. The environment variable's value is String.

Parameters

Name Description Type
variable_name The name of the environment variable whose value will be retrieved String

Raises

KeyError if the environment variable is not set.

Setting an Environment Variable's Value

set(variable_name, value)
EnvVar.set("SomeEnvVar", "some value")

Sets the value of the environment variable.

Returns

The value that was set. The type of the returned value is String.

Parameters

Name Description Type
variable_name The name of the environment variable whose value will be set String
value The value to assign to the environment variable String

Unsetting an Environment Variable's Value

unset(variable_name)
ENV["SomeEnvVar"] = "some value"
EnvVar.unset("SomeEnvVar")
# => "some value"

ENV["SomeEnvVar"]
# => nil

Unsets the environment variable.

Returns

The value of the environment variable before it was unset. The environment variable's type is String. If the environment variable was not set, the returned value is nil.

Parameters

Name Description Type
variable_name The name of the environment variable to remove String

Log Tags

The following tags are applied to log messages recorded by the EnvVar operations:

Tag Description
env_var Applied to all log messages written by this library

License

The EnvVar library is released under the MIT License.

About

Temporarily set environment variable values by treating the environment as a stack, and using blocks to implicitly control scope

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •