module Kernel
def capture(stream)
stream = capture(:stderr) { system('echo error 1>&2') }
stream # => "notice\n"
stream = capture(:stdout) { system('echo notice') }
even for subprocesses:
stream # => "error\n"
stream = capture(:stderr) { warn 'error' }
stream # => "notice\n"
stream = capture(:stdout) { puts 'notice' }
Captures the given stream and returns it:
def capture(stream) ActiveSupport::Deprecation.warn( "`#capture(stream)` is deprecated and will be removed in the next release." ) #not thread-safe stream = stream.to_s captured_stream = Tempfile.new(stream) stream_io = eval("$#{stream}") origin_stream = stream_io.dup stream_io.reopen(captured_stream) yield stream_io.rewind return captured_stream.read ensure captured_stream.close captured_stream.unlink stream_io.reopen(origin_stream) end
def class_eval(*args, &block)
def class_eval(*args, &block) singleton_class.class_eval(*args, &block) end
def concern(topic, &module_definition)
A shortcut to define a toplevel concern, not within a module.
def concern(topic, &module_definition) Object.concern topic, &module_definition end
def debugger
def debugger message = "\n***** Debugger requested, but was not available (ensure the debugger gem is listed in Gemfile/installed as gem): Start server with --debugger to enable *****\n" defined?(Rails.logger) ? Rails.logger.info(message) : $stderr.puts(message) end
def enable_warnings
Sets $VERBOSE to +true+ for the duration of the block and back to its
def enable_warnings with_warnings(true) { yield } end
def quietly
quietly { system 'bundle install' }
Silences both STDOUT and STDERR, even for subprocesses.
def quietly ActiveSupport::Deprecation.warn( "`#quietly` is deprecated and will be removed in the next release." ) #not thread-safe silence_stream(STDOUT) do silence_stream(STDERR) do yield end end end
def silence_stderr #:nodoc:
For compatibility
def silence_stderr #:nodoc: ActiveSupport::Deprecation.warn( "`#silence_stderr` is deprecated and will be removed in the next release." ) #not thread-safe silence_stream(STDERR) { yield } end
def silence_stream(stream)
puts 'But this will'
end
puts 'This will never be seen'
silence_stream(STDOUT) do
Silences any stream for the duration of the block.
Deprecated : this method is not thread safe
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) old_stream.close end
def silence_warnings
end
value = noisy_call # no warning voiced
silence_warnings do
value afterwards.
Sets $VERBOSE to nil for the duration of the block and back to its original
def silence_warnings with_warnings(nil) { yield } end
def suppress(*exception_classes)
end
puts 'This code is NOT reached'
1/0
suppress(ZeroDivisionError) do
Blocks and ignores any exception passed as argument if raised within the block.
def suppress(*exception_classes) yield rescue *exception_classes end
def with_warnings(flag)
Sets $VERBOSE for the duration of the block and back to its original
def with_warnings(flag) old_verbose, $VERBOSE = $VERBOSE, flag yield ensure $VERBOSE = old_verbose end