Sha256: 2365dd573b92794fd5f30e3a3c32548892e95c75c08f55c09454cab6dcb6c369

Contents?: true

Size: 1.59 KB

Versions: 41

Compression:

Stored size: 1.59 KB

Contents

require 'rbconfig'
module Kernel
  # 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
    with_warnings(nil) { yield }
  end

  # Sets $VERBOSE to true for the duration of the block and back to its original value afterwards.
  def enable_warnings
    with_warnings(true) { yield }
  end

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

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

  # 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(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null')
    stream.sync = true
    yield
  ensure
    stream.reopen(old_stream)
  end

  # Blocks and ignores any exception passed as argument if raised within the block.
  #
  #   suppress(ZeroDivisionError) do
  #     1/0
  #     puts "This code is NOT reached"
  #   end
  #
  #   puts "This code gets executed and nothing related to ZeroDivisionError was seen"
  def suppress(*exception_classes)
    begin yield
    rescue Exception => e
      raise unless exception_classes.any? { |cls| e.kind_of?(cls) }
    end
  end
end

Version data entries

41 entries across 41 versions & 3 rubygems

Version Path
social_url_stats-0.0.1 vendor/ruby/1.9.1/gems/activesupport-3.0.0/lib/active_support/core_ext/kernel/reporting.rb
activesupport-3.0.20 lib/active_support/core_ext/kernel/reporting.rb
activesupport-3.0.19 lib/active_support/core_ext/kernel/reporting.rb
activesupport-3.0.18 lib/active_support/core_ext/kernel/reporting.rb
activesupport-3.0.17 lib/active_support/core_ext/kernel/reporting.rb
activesupport-3.0.16 lib/active_support/core_ext/kernel/reporting.rb
activesupport-3.0.15 lib/active_support/core_ext/kernel/reporting.rb
activesupport-3.0.14 lib/active_support/core_ext/kernel/reporting.rb
activesupport-3.0.13 lib/active_support/core_ext/kernel/reporting.rb
activesupport-3.0.13.rc1 lib/active_support/core_ext/kernel/reporting.rb
activesupport-3.0.12 lib/active_support/core_ext/kernel/reporting.rb
activesupport-3.0.12.rc1 lib/active_support/core_ext/kernel/reporting.rb
activesupport-3.0.11 lib/active_support/core_ext/kernel/reporting.rb
messagebus_ruby_api-0.4.7 spec/ruby/1.9.1/gems/activesupport-3.0.9/lib/active_support/core_ext/kernel/reporting.rb
messagebus_ruby_api-0.4.4 spec/ruby/1.9.1/gems/activesupport-3.0.9/lib/active_support/core_ext/kernel/reporting.rb
activesupport-3.0.10 lib/active_support/core_ext/kernel/reporting.rb
activesupport-3.0.10.rc1 lib/active_support/core_ext/kernel/reporting.rb
activesupport-3.0.9 lib/active_support/core_ext/kernel/reporting.rb
activesupport-3.0.9.rc5 lib/active_support/core_ext/kernel/reporting.rb
activesupport-3.0.9.rc4 lib/active_support/core_ext/kernel/reporting.rb