File: with_env.rb

package info (click to toggle)
ruby-with-env 1.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 104 kB
  • sloc: ruby: 25; makefile: 4; sh: 3
file content (21 lines) | stat: -rw-r--r-- 349 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
require "with_env/version"

module WithEnv
  extend self

  def with_env(env, &blk)
    before = ENV.to_h.dup
    env.each { |k, v| ENV[k] = v }
    yield
  ensure
    ENV.replace(before)
  end

  def without_env(*keys, &blk)
    before = ENV.to_h.dup
    keys.flatten.each { |k| ENV.delete(k) }
    yield
  ensure
    ENV.replace(before)
  end
end