Module: Kernel
- Defined in:
- lib/gems/activesupport-2.2.2/lib/active_support/vendor/builder-2.1.2/blankslate.rb,
lib/mack-facets/extensions/kernel.rb,
lib/gems/activesupport-2.2.2/lib/active_support/core_ext/kernel/debugger.rb,
lib/gems/activesupport-2.2.2/lib/active_support/core_ext/kernel/requires.rb,
lib/gems/activesupport-2.2.2/lib/active_support/core_ext/kernel/reporting.rb,
lib/gems/activesupport-2.2.2/lib/active_support/core_ext/kernel/daemonizing.rb
Overview
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.
Class Method Summary collapse
- .blank_slate_method_added ⇒ Object
-
.method_added(name) ⇒ Object
Detect method additions to Kernel and remove them in the BlankSlate class.
Instance Method Summary collapse
-
#alias_class_method(orig_name, new_name = "_original_#{orig_name}") ⇒ Object
Aliases a class method to a new name.
-
#alias_instance_method(orig_name, new_name = "_original_#{orig_name}") ⇒ Object
Aliases an instance method to a new name.
- #breakpoint ⇒ Object
-
#daemonize ⇒ Object
Turns the current script into a daemon process that detaches from the console.
-
#debugger ⇒ Object
Starts a debugging session if ruby-debug has been loaded (call script/server –debugger to do load it).
-
#enable_warnings ⇒ Object
Sets $VERBOSE to true for the duration of the block and back to its original value afterwards.
- #pp_to_s(object) ⇒ Object
-
#require_library_or_gem(library_name) ⇒ Object
Require a library with fallback to RubyGems.
- #retryable(options = {}, &block) ⇒ Object
-
#ruby?(v) ⇒ Boolean
Returns true/false if the current version of Ruby equals the specified version.
- #run_once ⇒ Object
-
#silence_stderr ⇒ Object
For compatibility.
-
#silence_stream(stream) ⇒ Object
Silences any stream for the duration of the block.
-
#silence_warnings ⇒ Object
Sets $VERBOSE to nil for the duration of the block and back to its original value afterwards.
-
#suppress(*exception_classes) ⇒ Object
Blocks and ignores any exception passed as argument if raised within the block.
- #v1_8? ⇒ Boolean
- #v1_9? ⇒ Boolean
Class Method Details
.blank_slate_method_added ⇒ Object
61 |
# File 'lib/gems/activesupport-2.2.2/lib/active_support/vendor/builder-2.1.2/blankslate.rb', line 61 alias_method :blank_slate_method_added, :method_added |
.method_added(name) ⇒ Object
Detect method additions to Kernel and remove them in the BlankSlate class.
65 66 67 68 69 70 |
# File 'lib/gems/activesupport-2.2.2/lib/active_support/vendor/builder-2.1.2/blankslate.rb', line 65 def method_added(name) result = blank_slate_method_added(name) return result if self != Kernel BlankSlate.hide(name) result end |
Instance Method Details
#alias_class_method(orig_name, new_name = "_original_#{orig_name}") ⇒ Object
Aliases a class method to a new name. It will only do the aliasing once, to prevent issues with reloading a class and causing a StackLevel too deep error. The method takes two arguments, the first is the original name of the method, the second, optional, parameter is the new name of the method. If you don’t specify a new method name it will be generated with original<original_name>.
Example:
class President
alias_class_method :good
alias_class_method :bad, :old_bad
def self.good
'Bill ' + _original_good
end
def self.bad
"Either #{old_bad}"
end
end
53 54 55 56 57 58 59 |
# File 'lib/mack-facets/extensions/kernel.rb', line 53 def alias_class_method(orig_name, new_name = "_original_#{orig_name}") eval(%{ class << self alias_method :#{new_name}, :#{orig_name} unless method_defined?("#{new_name}") end }) end |
#alias_instance_method(orig_name, new_name = "_original_#{orig_name}") ⇒ Object
Aliases an instance method to a new name. It will only do the aliasing once, to prevent issues with reloading a class and causing a StackLevel too deep error. The method takes two arguments, the first is the original name of the method, the second, optional, parameter is the new name of the method. If you don’t specify a new method name it will be generated with original<original_name>.
Example:
class Popcorn < Corn
alias_instance_method :poppy
alias_instance_method :corny, :old_corny
def poppy
2 * _original_poppy
end
def corny
'pop' + old_corny
end
end
32 33 34 |
# File 'lib/mack-facets/extensions/kernel.rb', line 32 def alias_instance_method(orig_name, new_name = "_original_#{orig_name}") alias_method new_name.to_sym, orig_name.to_sym unless method_defined?(new_name.to_s) end |
#breakpoint ⇒ Object
9 10 11 12 |
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/kernel/debugger.rb', line 9 def breakpoint Rails.logger.info "\n***** The 'breakpoint' command has been renamed 'debugger' -- please change *****\n" debugger end |
#daemonize ⇒ Object
Turns the current script into a daemon process that detaches from the console. It can be shut down with a TERM signal.
4 5 6 |
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/kernel/daemonizing.rb', line 4 def daemonize Process.daemon end |
#debugger ⇒ Object
Starts a debugging session if ruby-debug has been loaded (call script/server –debugger to do load it).
4 5 6 |
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/kernel/debugger.rb', line 4 def debugger Rails.logger.info "\n***** Debugger requested, but was not available: Start server with --debugger to enable *****\n" end |
#enable_warnings ⇒ Object
Sets $VERBOSE to true for the duration of the block and back to its original value afterwards.
17 18 19 20 21 22 |
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/kernel/reporting.rb', line 17 def enable_warnings old_verbose, $VERBOSE = $VERBOSE, true yield ensure $VERBOSE = old_verbose end |
#pp_to_s(object) ⇒ Object
61 62 63 64 65 |
# File 'lib/mack-facets/extensions/kernel.rb', line 61 def pp_to_s(object) pp_out = StringIO.new PP.pp(object,pp_out) return pp_out.string end |
#require_library_or_gem(library_name) ⇒ Object
Require a library with fallback to RubyGems. Warnings during library loading are silenced to increase signal/noise for application warnings.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/kernel/requires.rb', line 4 def require_library_or_gem(library_name) silence_warnings do begin require library_name rescue LoadError => cannot_require # 1. Requiring the module is unsuccessful, maybe it's a gem and nobody required rubygems yet. Try. begin require 'rubygems' rescue LoadError => rubygems_not_installed raise cannot_require end # 2. Rubygems is installed and loaded. Try to load the library again begin require library_name rescue LoadError => gem_not_installed raise cannot_require end end end end |
#retryable(options = {}, &block) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/mack-facets/extensions/kernel.rb', line 67 def retryable( = {}, &block) opts = { :tries => 1, :on => Exception }.merge() retries = opts[:tries] retry_exceptions = [opts[:on]].flatten x = %{ begin return yield rescue #{retry_exceptions.join(", ")} => e retries -= 1 if retries > 0 retry else raise e end end } eval(x, &block) end |
#ruby?(v) ⇒ Boolean
Returns true/false if the current version of Ruby equals the specified version
90 91 92 |
# File 'lib/mack-facets/extensions/kernel.rb', line 90 def ruby?(v) RUBY_VERSION == v end |
#run_once ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/mack-facets/extensions/kernel.rb', line 6 def run_once path = File.(caller.first) unless ($__already_run_block ||= []).include?(path) yield $__already_run_block << path end # puts "$__already_run_block: #{$__already_run_block.inspect}" end |
#silence_stderr ⇒ Object
For compatibility
25 26 27 |
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/kernel/reporting.rb', line 25 def silence_stderr #:nodoc: silence_stream(STDERR) { yield } end |
#silence_stream(stream) ⇒ Object
Silences any stream for the duration of the block.
silence_stream(STDOUT) do
puts 'This will never be seen'
end
puts 'But this will'
36 37 38 39 40 41 42 43 |
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/kernel/reporting.rb', line 36 def silence_stream(stream) old_stream = stream.dup stream.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null') stream.sync = true yield ensure stream.reopen(old_stream) end |
#silence_warnings ⇒ Object
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
9 10 11 12 13 14 |
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/kernel/reporting.rb', line 9 def silence_warnings old_verbose, $VERBOSE = $VERBOSE, nil yield ensure $VERBOSE = old_verbose end |
#suppress(*exception_classes) ⇒ Object
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"
53 54 55 56 57 58 |
# File 'lib/gems/activesupport-2.2.2/lib/active_support/core_ext/kernel/reporting.rb', line 53 def suppress(*exception_classes) begin yield rescue Exception => e raise unless exception_classes.any? { |cls| e.kind_of?(cls) } end end |
#v1_8? ⇒ Boolean
94 95 96 |
# File 'lib/mack-facets/extensions/kernel.rb', line 94 def v1_8? RUBY_VERSION >= '1.8.0' && RUBY_VERSION < '1.9.0' end |
#v1_9? ⇒ Boolean
98 99 100 |
# File 'lib/mack-facets/extensions/kernel.rb', line 98 def v1_9? RUBY_VERSION >= '1.9.0' && RUBY_VERSION < '2.0.0' end |