Method: Dotenv#restore

Defined in:
lib/dotenv.rb

#restore(env = @diff&.a, safe: Thread.current == Thread.main) ⇒ Object

Restore ‘ENV` to a given state

Parameters:

  • env (Hash) (defaults to: @diff&.a)

    Hash of keys and values to restore, defaults to the last saved state

  • safe (Boolean) (defaults to: Thread.current == Thread.main)

    Is it safe to modify ‘ENV`? Defaults to `true` in the main thread, otherwise raises an error.



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dotenv.rb', line 76

def restore(env = @diff&.a, safe: Thread.current == Thread.main)
  diff = Dotenv::Diff.new(b: env)
  return unless diff.any?

  unless safe
    raise ThreadError, <<~EOE.tr("\n", " ")
      Dotenv.restore is not thread safe. Use `Dotenv.modify { }` to update ENV for the duration
      of the block in a thread safe manner, or call `Dotenv.restore(safe: true)` to ignore
      this error.
    EOE
  end
  instrument(:restore, diff: diff) { ENV.replace(env) }
end