Method: Dotenv#modify

Defined in:
lib/dotenv.rb

#modify(env = {}, &block) ⇒ Object

Modify ‘ENV` for the block and restore it to its previous state afterwards.

Note that the block is synchronized to prevent concurrent modifications to ‘ENV`, so multiple threads will be executed serially.

Parameters:

  • env (Hash) (defaults to: {})

    Hash of keys and values to set in ‘ENV`



112
113
114
115
116
117
118
119
120
# File 'lib/dotenv.rb', line 112

def modify(env = {}, &block)
  SEMAPHORE.synchronize do
    diff = Dotenv::Diff.new
    update(env, overwrite: true)
    block.call
  ensure
    restore(diff.a, safe: true)
  end
end