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
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 |
Pushing a Change to a Set of Environment Variables
self.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 |
Getting an Environment Variable's Value
self.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
self.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
self.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
self.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 |
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.