Captures the given stream and returns it:
stream = capture(:stdout) { puts "Cool" } stream # => "Cool\n"
# File lib/active_support/core_ext/kernel/reporting.rb, line 68 68: def capture(stream) 69: begin 70: stream = stream.to_s 71: eval "$#{stream} = StringIO.new" 72: yield 73: result = eval("$#{stream}").string 74: ensure 75: eval("$#{stream} = #{stream.upcase}") 76: end 77: 78: result 79: end
class_eval on an object acts like singleton_class.class_eval.
# File lib/active_support/core_ext/kernel/singleton_class.rb, line 10 10: def class_eval(*args, &block) 11: singleton_class.class_eval(*args, &block) 12: end
Starts a debugging session if ruby-debug has been loaded (call rails server —debugger to do load it).
# File lib/active_support/core_ext/kernel/debugger.rb, line 4 4: def debugger 5: message = "\n***** Debugger requested, but was not available (ensure ruby-debug is listed in Gemfile/installed as gem): Start server with --debugger to enable *****\n" 6: defined?(Rails) ? Rails.logger.info(message) : $stderr.puts(message) 7: end
Sets $VERBOSE to true for the duration of the block and back to its original value afterwards.
# File lib/active_support/core_ext/kernel/reporting.rb, line 15 15: def enable_warnings 16: with_warnings(true) { yield } 17: end
Silences both STDOUT and STDERR, even for subprocesses.
quietly { system 'bundle install' }
# File lib/active_support/core_ext/kernel/reporting.rb, line 86 86: def quietly 87: silence_stream(STDOUT) do 88: silence_stream(STDERR) do 89: yield 90: end 91: end 92: 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'
# File lib/active_support/core_ext/kernel/reporting.rb, line 39 39: def silence_stream(stream) 40: old_stream = stream.dup 41: stream.reopen(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null') 42: stream.sync = true 43: yield 44: ensure 45: stream.reopen(old_stream) 46: 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
# File lib/active_support/core_ext/kernel/reporting.rb, line 10 10: def silence_warnings 11: with_warnings(nil) { yield } 12: end
Returns the object’s singleton class.
# File lib/active_support/core_ext/kernel/singleton_class.rb, line 3 3: def singleton_class 4: class << self 5: self 6: end 7: 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"
# File lib/active_support/core_ext/kernel/reporting.rb, line 56 56: def suppress(*exception_classes) 57: begin yield 58: rescue Exception => e 59: raise unless exception_classes.any? { |cls| e.kind_of?(cls) } 60: end 61: end
Sets $VERBOSE for the duration of the block and back to its original value afterwards.
# File lib/active_support/core_ext/kernel/reporting.rb, line 20 20: def with_warnings(flag) 21: old_verbose, $VERBOSE = $VERBOSE, flag 22: yield 23: ensure 24: $VERBOSE = old_verbose 25: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.