Sha256: 5a5545cc2550490652a78581611a7f3aad85b977282341e20e814861a549562f

Contents?: true

Size: 1.77 KB

Versions: 10

Compression:

Stored size: 1.77 KB

Contents

# TITLE:
#   Verbosity Reporting Extensions
#
# DESCRIPTION:
#   Kernel extensions dealing with verbosity of warnings
#   and error messages.
#
# AUTHORS:
#   - David Heinemeier Hansson
#   - TransSilentNight
#
# LOG:
#   - trans 07.09.01 Deprecated #silently as alias of slience_warnings.

module Kernel
  module_function

  # Silences any stream for the duration of the block.
  #
  #   silence_stream(STDOUT) do
  #     puts 'This will never be seen'
  #   end
  #
  #   puts 'But this will'
  def silence_stream(stream)
    old_stream = stream.dup
    stream.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null')
    stream.sync = true
    yield
  ensure
    stream.reopen(old_stream)
  end
  #alias_method :silently, :silence_stream

  # For compatibility
  def silence_stderr #:nodoc:
    silence_stream(STDERR) { yield }
  end

  # Sets $VERBOSE to nil for the duration of the block
  # and back to its original value afterwards.
  #
  #   silence_warnings do
  #     value = noisy_call # no warning voiced
  #   end
  #
  #   noisy_call # warning voiced
  def silence_warnings
    old_verbose, $VERBOSE = $VERBOSE, nil
    yield
  ensure
    $VERBOSE = old_verbose
  end

  # Sets $VERBOSE to true for the duration of the block
  # and back to its original value afterwards.
  def enable_warnings
    old_verbose, $VERBOSE = $VERBOSE, true
    yield
  ensure
    $VERBOSE = old_verbose
  end

  # Supress errors while executing a block, with execptions.

  def suppress(*exception_classes)
    begin yield
    rescue Exception => e
      raise unless exception_classes.any? { |cls| e.kind_of?(cls) }
    end
  end

end



=begin test

  require 'test/unit'

  class TestKernelError < Test::Unit::TestCase

    def test_silently
      silently do
        assert( ! $VERBOSE )
      end
    end

  end

=end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
facets-2.0.0 lib/core/facets/kernel/silence.rb
facets-2.0.1 lib/core/facets/kernel/silence.rb
facets-2.0.2 lib/core/facets/kernel/silence.rb
facets-2.1.1 lib/core/facets/kernel/silence.rb
facets-2.0.3 lib/core/facets/kernel/silence.rb
facets-2.0.5 lib/core/facets/kernel/silence.rb
facets-2.0.4 lib/core/facets/kernel/silence.rb
facets-2.1.0 lib/core/facets/kernel/silence.rb
facets-2.1.2 lib/core/facets/kernel/silence.rb
facets-2.1.3 lib/core/facets/kernel/silence.rb