Sha256: 9309e6a3a572fe9b2238b2a4f0b844c755537292cf386c892dc92023874eba00

Contents?: true

Size: 519 Bytes

Versions: 10

Compression:

Stored size: 519 Bytes

Contents

module Kernel

  # Temporarily set variables while yielding a block, then return the
  # variables to their original settings when complete.
  #
  #   temporarily('$VERBOSE'=>false) do
  #     $VERBOSE.assert == false
  #   end
  #
  def temporarily(settings) #:yield:
    cache = {}
    settings.each do |var, val|
      cache[var] = eval("#{var}")
      eval("proc{ |v| #{var} = v }").call(val)
    end
    yield
  ensure
    cache.each do |var, val|
      eval("proc{ |v| #{var} = v }").call(val)
    end
  end

end

Version data entries

10 entries across 9 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/kernel/temporarily.rb
facets-3.1.0 lib/core/facets/kernel/temporarily.rb
facets-3.0.0 lib/core/facets/kernel/temporarily.rb
facets-2.9.3 lib/core/facets/kernel/temporarily.rb
facets-2.9.2 src/core/facets/kernel/temporarily.rb
facets-2.9.2 lib/core/facets/kernel/temporarily.rb
facets-2.9.1 lib/core/facets/kernel/temporarily.rb
facets-2.9.0 lib/core/facets/kernel/temporarily.rb
facets-2.9.0.pre.2 lib/core/facets/kernel/temporarily.rb
facets-2.9.0.pre.1 lib/core/facets/kernel/temporarily.rb