Since Ruby is very dynamic, methods added to the ancestors of BlankSlate after BlankSlate is defined will show up in the list of available BlankSlate methods. We handle this by defining a hook in the Object and Kernel classes that will hide any method defined after BlankSlate has been loaded.
method_added | -> | blank_slate_method_added |
Detect method additions to Kernel and remove them in the BlankSlate class.
# File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/blankslate.rb, line 65 65: def method_added(name) 66: result = blank_slate_method_added(name) 67: return result if self != Kernel 68: BlankSlate.hide(name) 69: result 70: end
# File vendor/rails/activesupport/lib/active_support/core_ext/kernel/debugger.rb, line 9 9: def breakpoint 10: Rails.logger.info "\n***** The 'breakpoint' command has been renamed 'debugger' -- please change *****\n" 11: debugger 12: end
Turns the current script into a daemon process that detaches from the console. It can be shut down with a TERM signal.
# File vendor/rails/activesupport/lib/active_support/core_ext/kernel/daemonizing.rb, line 4 4: def daemonize 5: Process.daemon 6: end
Starts a debugging session if ruby-debug has been loaded (call script/server —debugger to do load it).
# File vendor/rails/activesupport/lib/active_support/core_ext/kernel/debugger.rb, line 4 4: def debugger 5: Rails.logger.info "\n***** Debugger requested, but was not available: Start server with --debugger to enable *****\n" 6: end
Sets $VERBOSE to true for the duration of the block and back to its original value afterwards.
# File vendor/rails/activesupport/lib/active_support/core_ext/kernel/reporting.rb, line 17 17: def enable_warnings 18: old_verbose, $VERBOSE = $VERBOSE, true 19: yield 20: ensure 21: $VERBOSE = old_verbose 22: end
Require a library with fallback to RubyGems. Warnings during library loading are silenced to increase signal/noise for application warnings.
# File vendor/rails/activesupport/lib/active_support/core_ext/kernel/requires.rb, line 4 4: def require_library_or_gem(library_name) 5: silence_warnings do 6: begin 7: require library_name 8: rescue LoadError => cannot_require 9: # 1. Requiring the module is unsuccessful, maybe it's a gem and nobody required rubygems yet. Try. 10: begin 11: require 'rubygems' 12: rescue LoadError => rubygems_not_installed 13: raise cannot_require 14: end 15: # 2. Rubygems is installed and loaded. Try to load the library again 16: begin 17: require library_name 18: rescue LoadError => gem_not_installed 19: raise cannot_require 20: end 21: end 22: end 23: 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 vendor/rails/activesupport/lib/active_support/core_ext/kernel/reporting.rb, line 36 36: def silence_stream(stream) 37: old_stream = stream.dup 38: stream.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null') 39: stream.sync = true 40: yield 41: ensure 42: stream.reopen(old_stream) 43: 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 vendor/rails/activesupport/lib/active_support/core_ext/kernel/reporting.rb, line 9 9: def silence_warnings 10: old_verbose, $VERBOSE = $VERBOSE, nil 11: yield 12: ensure 13: $VERBOSE = old_verbose 14: 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 vendor/rails/activesupport/lib/active_support/core_ext/kernel/reporting.rb, line 53 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