Module: Dotenv::Substitutions::Variable

Defined in:
lib/dotenv/substitutions/variable.rb

Overview

Substitute variables in a value.

HOST=example.com
URL="https://$HOST"

Constant Summary collapse

VARIABLE =
/
  (\\)?         # is it escaped with a backslash?
  (\$)          # literal $
  (?!\()        # shouldnt be followed by paranthesis
  \{?           # allow brace wrapping
  ([A-Z0-9_]+)? # optional alpha nums
  \}?           # closing brace
/xi

Class Method Summary collapse

Class Method Details

.call(value, env, overwrite: false) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/dotenv/substitutions/variable.rb', line 21

def call(value, env, overwrite: false)
  combined_env = overwrite ? ENV.to_h.merge(env) : env.merge(ENV)
  value.gsub(VARIABLE) do |variable|
    match = $LAST_MATCH_INFO
    substitute(match, variable, combined_env)
  end
end