Sha256: e681408d5c7da68f355062914ff11176494516388e501f22c580cf448ebaf391

Contents?: true

Size: 865 Bytes

Versions: 2

Compression:

Stored size: 865 Bytes

Contents

module Kernel
  class << self

    def with_warnings(flag, &block)
      old_verbose, $VERBOSE = $VERBOSE, flag
      yield
    ensure
      $VERBOSE = old_verbose
    end

    def silence_warnings(&block)
      with_warnings(nil, &block)
    end

  end
end

class Object
  def self.with_constants(constants, &block)
    old_constants = Hash.new
    missing_constants = []

    constants.each do |constant, val|
      if const_defined?(constant)
        old_constants[constant] = const_get(constant)
      else
        missing_constants << constant
      end

      Kernel::silence_warnings{ const_set(constant, val) }
    end

    yield

    old_constants.each do |constant, val|
      Kernel::silence_warnings{ const_set(constant, val) }
    end

    missing_constants.each do |constant|
      Kernel::silence_warnings{ remove_const(constant) }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
koality-1.0.1 spec/support/with_constants.rb
koality-1.0.0 spec/support/with_constants.rb