Method: Dotenv#update

Defined in:
lib/dotenv.rb

#update(env = {}, overwrite: false) ⇒ Object

Update ‘ENV` with the given hash of keys and values



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/dotenv.rb', line 98

def update(env = {}, overwrite: false)
  instrument(:update) do |payload|
    diff = payload[:diff] = Dotenv::Diff.new do
      ENV.update(env.transform_keys(&:to_s)) do |key, old_value, new_value|
        # This block is called when a key exists. Return the new value if overwrite is true.
        case overwrite
        when :warn
          # not printing the value since that could be a secret
          warn "Warning: dotenv not overwriting ENV[#{key.inspect}]"
          old_value
        when true then new_value
        when false then old_value
        else raise ArgumentError, "Invalid value for overwrite: #{overwrite.inspect}"
        end
      end
    end
    diff.env
  end
end