File: active_support/core_ext/kernel/reporting.rb

Overview
Module Structure
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: <Built-in Module>
  module: Kernel#1
has properties
method: silence_warnings #9
method: enable_warnings #17
method: silence_stderr #25
method: silence_stream / 1 #36
method: suppress / 1 #53

Code

   1  module Kernel
   2    # Sets $VERBOSE to nil for the duration of the block and back to its original value afterwards.
   3    #
   4    #   silence_warnings do
   5    #     value = noisy_call # no warning voiced
   6    #   end
   7    #
   8    #   noisy_call # warning voiced
   9    def silence_warnings
  10      old_verbose, $VERBOSE = $VERBOSE, nil
  11      yield
  12    ensure
  13      $VERBOSE = old_verbose
  14    end
  15 
  16    # Sets $VERBOSE to true for the duration of the block and back to its original value afterwards.
  17    def enable_warnings
  18      old_verbose, $VERBOSE = $VERBOSE, true
  19      yield
  20    ensure
  21      $VERBOSE = old_verbose
  22    end
  23 
  24    # For compatibility
  25    def silence_stderr #:nodoc:
  26      silence_stream(STDERR) { yield }
  27    end
  28 
  29    # Silences any stream for the duration of the block.
  30    #
  31    #   silence_stream(STDOUT) do
  32    #     puts 'This will never be seen'
  33    #   end
  34    #
  35    #   puts 'But this will'
  36    def silence_stream(stream)
  37      old_stream = stream.dup
  38      stream.reopen(RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'NUL:' : '/dev/null')
  39      stream.sync = true
  40      yield
  41    ensure
  42      stream.reopen(old_stream)
  43    end
  44 
  45    # Blocks and ignores any exception passed as argument if raised within the block.
  46    #
  47    #   suppress(ZeroDivisionError) do
  48    #     1/0
  49    #     puts "This code is NOT reached"
  50    #   end
  51    #
  52    #   puts "This code gets executed and nothing related to ZeroDivisionError was seen"
  53    def suppress(*exception_classes)
  54      begin yield
  55      rescue Exception => e
  56        raise unless exception_classes.any? { |cls| e.kind_of?(cls) }
  57      end
  58    end
  59  en