module Dotenv

Extended Modules

Defined in:

dotenv.cr

Class Method Summary

Instance Method Summary

Class Method Detail

def self.strict : Bool #

Raises an exception on parsing error.


[View source]
def self.strict=(strict : Bool) #

Raises an exception on parsing error.


[View source]

Instance Method Detail

def load(filename : Path | String = ".env", override_keys : Bool = false) : Hash(String, String) #

Loads environment variables from a file into the ENV constant.

require "dotenv"

File.write ".env-file", "VAR=Hello"
Dotenv.load ".env-file"    # => {"VAR" => "Hello"}
Dotenv.load ".absent-file" # => No such file or directory (Errno)

[View source]
def load(io : IO, override_keys : Bool = false) : Hash(String, String) #

Loads environment variables from an IO into the ENV constant.

require "dotenv"

hash = Dotenv.load IO::Memory.new("VAR=Hello")
hash # => {"VAR" => "Hello"}

[View source]
def load(hash : Hash(String, String), override_keys : Bool = false) : Hash(String, String) #

Loads a Hash of environment variables into the ENV constant.

require "dotenv"

Dotenv.load({"VAR" => "test"})

[View source]
def load?(filename : Path | String = ".env", override_keys : Bool = false) : Hash(String, String) | Nil #

Loads environment variables from a file into the ENV constant if the file is present, else returns nil.

require "dotenv"

File.write ".env-file", "VAR=Hello"
Dotenv.load? ".env-file"    # => {"VAR" => "Hello"}
Dotenv.load? ".not-present" # => nil

[View source]
def load_string(env_vars : String, override_keys : Bool = false) : Hash(String, String) #

Loads environment variables from a String into the ENV constant.

require "dotenv"

hash = Dotenv.load_string "VAR=Hello"
hash # => {"VAR" => "Hello"}

[View source]
def parse(env_vars : String | IO) : Hash(String, String) #

Parses a .env formatted String/IO data, and returns a hash (without loading it to ENV).

require "dotenv"

hash = Dotenv.parse "VAR=Hello"
hash # => {"VAR" => "Hello"}

[View source]