module ENV
Overview
ENV is a hash-like accessor for environment variables.
Example
# Set env var PORT to a default if not already set
ENV["PORT"] ||= "5000"
# Later use that env var.
puts ENV["PORT"].to_i
NOTE All keys and values are strings. You must take care to cast other types at runtime, e.g. integer port numbers.
Extended Modules
- Enumerable({String, String})
Defined in:
env.crClass Method Summary
-
.[](key : String) : String
Retrieves the value for environment variable namedkey as a
String. -
.[]=(key : String, value : String | Nil)
Sets the value for environment variable namedkey asvalue.
-
.[]?(key : String) : String | Nil
Retrieves the value for environment variable namedkey as a
String?. - .clear : Nil
-
.delete(key : String) : String | Nil
Removes the environment variable namedkey.
-
.each(& : Tuple(String, String) -> )
Iterates over all
KEY=VALUEpairs of environment variables, yielding both thekey andvalue. -
.fetch(key, default : T) : String | T forall T
Retrieves a value corresponding to the givenkey.
-
.fetch(key) : String
Retrieves a value corresponding to the givenkey.
-
.fetch(key : String, &block : String -> T) : String | T forall T
Retrieves a value corresponding to a givenkey.
-
.has_key?(key : String) : Bool
Returns
trueif the environment variable namedkey exists andfalseif it doesn't. -
.inspect(io)
Writes the contents of the environment toio.
-
.keys : Array(String)
Returns an array of all the environment variable names.
- .pretty_print(pp)
-
.values : Array(String)
Returns an array of all the environment variable values.
Class Method Detail
Retrieves the value for environment variable namedkey as aString.
RaisesKeyError if the named variable does not exist.
Sets the value for environment variable namedkey asvalue.
Overwrites existing environment variable if already present.
Returnsvalue if successful, otherwise raises an exception.
Ifvalue isnil, the environment variable is deleted.
Ifkey orvalue contains a null-byte anArgumentError is raised.
Retrieves the value for environment variable namedkey as aString?.
Returnsnil if the named variable does not exist.
Removes the environment variable namedkey. Returns the previous value if
the environment variable existed, otherwise returnsnil.
Iterates over allKEY=VALUE pairs of environment variables, yielding both
thekey andvalue.
ENV.each do |key, value|
puts "#{key} => #{value}"
end
Retrieves a value corresponding to the givenkey. Return the second argument's value if thekey does not exist.
Retrieves a value corresponding to the givenkey. Raises aKeyError exception if the
key does not exist.
Retrieves a value corresponding to a givenkey. Return the value of the block if thekey does not exist.
Returnstrue if the environment variable namedkey exists andfalse if it doesn't.
ENV.has_key?("NOT_A_REAL_KEY") # => false
ENV.has_key?("PATH") # => true