module ENV

Overview

ENV is a hash-like accessor for environment variables.

Note: All keys and values are strings. You must take care to cast other types at runtime, e.g. integer port numbers.

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

Defined in:

env.cr

Class Method Summary

Class Method Detail

def self.[](key : String) #

Retrieves the value for environment variable named key as a String. Raises KeyError if the named variable does not exist.


[View source]
def self.[]=(key : String, value : String) #

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.


[View source]
def self.[]?(key : String) #

Retrieves the value for environment variable named key as a String?. Returns nil if the named variable does not exist.


[View source]
def self.clear #

[View source]
def self.delete(key : String) #

Removes the environment variable named key. Returns the previous value if the environment variable existed, otherwise returns nil.


[View source]
def self.each(&block) #

Iterates over all KEY=VALUE pairs of environment variables, yielding both the key and value.

ENV.each do |key, value|
  puts "#{key} => #{value}"
end

[View source]
def self.fetch(key : String, &block : String -> String | Nil | NoReturn) #

Retrieves a value corresponding to a key. Return the value of the block if the key does not exist.


[View source]
def self.fetch(key) #

Retrieves a value corresponding to the given key. Raises a KeyError exception if the key does not exist.


[View source]
def self.fetch(key, default) #

Retrieves a value corresponding to the given key. Return the second argument's value if the key does not exist.


[View source]
def self.has_key?(key : String) #

Returns true if the environment variable named key exists and false if it doesn't.


[View source]
def self.inspect(io) #

Writes the contents of the environment to io.


[View source]
def self.keys #

Returns an array of all the environment variable names.


[View source]
def self.values #

Returns an array of all the environment variable values.


[View source]