ENV
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 named key as a
String
. - .[]=(key : String, value : String?)
Sets the value for environment variable named key as value.
- .[]?(key : String) : String?
Retrieves the value for environment variable named key as a
String?
. - .clear
- .delete(key : String) : String?
Removes the environment variable named key.
- .each(&block)
Iterates over all
KEY=VALUE
pairs of environment variables, yielding both the key and value. - .fetch(key, default)
Retrieves a value corresponding to the given key.
- .fetch(key) : String
Retrieves a value corresponding to the given key.
- .fetch(key : String, &block : String -> String? | NoReturn)
Retrieves a value corresponding to a given key.
- .has_key?(key : String) : Bool
Returns
true
if the environment variable named key exists andfalse
if it doesn't. - .inspect(io)
Writes the contents of the environment to io.
- .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
def self.[]=(key : String, value : String?)Source
Sets the value for environment variable named key as value. Overwrites existing environment variable if already present. Returns value if successful, otherwise raises an exception. If value is nil
, the environment variable is deleted.
def self.[]?(key : String) : String?Source
Retrieves the value for environment variable named key as a String?
. Returns nil
if the named variable does not exist.
def self.clearSource
def self.delete(key : String) : String?Source
Removes the environment variable named key. Returns the previous value if the environment variable existed, otherwise returns nil
.
def self.each(&block)Source
Iterates over all KEY=VALUE
pairs of environment variables, yielding both the key and value.
ENV.each do |key, value| puts "#{key} => #{value}" end
def self.fetch(key, default)Source
Retrieves a value corresponding to the given key. Return the second argument's value if the key does not exist.
def self.fetch(key) : StringSource
Retrieves a value corresponding to the given key. Raises a KeyError
exception if the key does not exist-
def self.fetch(key : String, &block : String -> String? | NoReturn)Source
Retrieves a value corresponding to a given key. Return the value of the block if the key does not exist.
def self.has_key?(key : String) : BoolSource
Returns true
if the environment variable named key exists and false
if it doesn't.
def self.inspect(io)Source
Writes the contents of the environment to io.
def self.pretty_print(pp)Source
© 2012–2017 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.22.0/ENV.html