Temporarily set environment variable values by treating the environment as a stack, and using blocks to control scope
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"push(variable_name, value, &action)EnvVar.push("SomeEnvVar", "some new value") do
# The SomeEnvVar environment value is
# "some new value" inside this block
endThe 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 |
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
endThe 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 |
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 |
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.
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 |
unset(variable_name)ENV["SomeEnvVar"] = "some value"
EnvVar.unset("SomeEnvVar")
# => "some value"
ENV["SomeEnvVar"]
# => nilUnsets 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 |
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 |
The EnvVar library is released under the MIT License.