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`



116
117
118
119
120
121
122
123
124
# File 'lib/dotenv.rb', line 116

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