Module: Kernel
- Defined in:
- lib/qualitysmith_extensions/kernel/die.rb,
lib/qualitysmith_extensions/object/default.rb,
lib/qualitysmith_extensions/kernel/backtrace.rb,
lib/qualitysmith_extensions/kernel/trap_chain.rb,
lib/qualitysmith_extensions/kernel/require_all.rb,
lib/qualitysmith_extensions/global_variable_set.rb,
lib/qualitysmith_extensions/kernel/require_once.rb,
lib/qualitysmith_extensions/kernel/capture_output.rb
Overview
–
- Author
-
Tyler Rick
- Copyright
-
Copyright © 2007 QualitySmith, Inc.
- License
-
Ruby License
- Submit to Facets?
-
No, too ugly and unreliable.
++
Instance Method Summary collapse
-
#backtrace ⇒ Object
Equivalent to calling caller(0).
-
#backtrace_using_exception ⇒ Object
I thought I ran into some case where it didn’t work to use caller(0)…which prompted me to do it this way (raise and rescue an exception)…but now I can’t duplicate that problem, so I will deprecate this method.
-
#capture_output(output_streams = $stdout, &block) ⇒ Object
Captures the output (stdout by default) that
blocktries to generate and returns it as a string. - #default!(object, default_value) ⇒ Object
- #die(message, exit_code = 1) ⇒ Object
-
#global_variable_get(var, options = {}) ⇒ Object
Gets the global variable
var, which can either be a symbol or an actual global variable (use:match_object). -
#global_variable_name(var) ⇒ Object
Looks up the name of global variable
var, which must be an actual global variable. -
#global_variable_set(var, value, options = {}) ⇒ Object
Sets the global variable
var, which can either be a symbol or an actual global variable (use:match_object). -
#pretty_backtrace ⇒ Object
Returns a human-readable backtrace.
-
#require_all(base_dir, options = {}) ⇒ Object
Recursively
requires all Ruby files inbase_diror any of its subdirectories. -
#require_once(name) ⇒ Object
Fixes bug in Ruby (1.8, at least – not sure if 2.0 fixes it) where a file can be required twice if the path is spelled differently.
-
#trap_chain(signal_name, *args, &block) ⇒ Object
Calling
Kernel#trap()by itself will replace any previously registered handler code.
Instance Method Details
#backtrace ⇒ Object
Equivalent to calling caller(0)
13 14 15 16 |
# File 'lib/qualitysmith_extensions/kernel/backtrace.rb', line 13 def backtrace full_backtrace = caller(0) return full_backtrace[1..-1] # We don't want this call to backtrace showing up in the backtrace, so skip top 1 frame. end |
#backtrace_using_exception ⇒ Object
I thought I ran into some case where it didn’t work to use caller(0)…which prompted me to do it this way (raise and rescue an exception)…but now I can’t duplicate that problem, so I will deprecate this method.
25 26 27 28 29 30 31 32 |
# File 'lib/qualitysmith_extensions/kernel/backtrace.rb', line 25 def backtrace_using_exception begin raise "Where am I?" rescue Exception => exception full_backtrace = exception.backtrace return full_backtrace[1..-1] # We don't want this call to backtrace showing up in the backtrace, so skip top 1 frame. end end |
#capture_output(output_streams = $stdout, &block) ⇒ Object
Captures the output (stdout by default) that block tries to generate and returns it as a string.
output = capture_output($stderr) { noisy_command }
output = capture_output([$stdout, $stderr]) do
noisy_command
end
Note: If you specify more than one output stream, the entire results of each will be concatenated in the order you listed them, not necessarily in the order that you wrote to those streams.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/qualitysmith_extensions/kernel/capture_output.rb', line 35 def capture_output(output_streams = $stdout, &block) output_streams = [output_streams] unless output_streams.is_a? Array saved_output_streams = Dictionary.new output_streams.each do |output_stream| case output_stream.object_id when $stdout.object_id saved_output_streams[:$stdout] = $stdout $stdout = StringIO.new when $stderr.object_id saved_output_streams[:$stderr] = $stderr $stderr = StringIO.new end end what_they_tried_to_output = '' begin yield rescue Exception raise ensure saved_output_streams.each do |name, output_stream| case name when :$stdout what_they_tried_to_output += $stdout.string when :$stderr what_they_tried_to_output += $stderr.string end # Restore the original output_stream that we saved. case name when :$stdout $stdout = saved_output_streams[:$stdout] when :$stderr $stderr = saved_output_streams[:$stderr] end end end return what_they_tried_to_output end |
#default!(object, default_value) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/qualitysmith_extensions/object/default.rb', line 9 def default!(object, default_value) case object when NilClass #object.become default_value #object.replace default_value else end end |
#die(message, exit_code = 1) ⇒ Object
10 11 12 13 |
# File 'lib/qualitysmith_extensions/kernel/die.rb', line 10 def die(, exit_code = 1) $stderr.puts exit exit_code end |
#global_variable_get(var, options = {}) ⇒ Object
Gets the global variable var, which can either be a symbol or an actual global variable (use :match_object).
global_variable_get(:$a)
global_variable_get($a, :match_object => true)
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/qualitysmith_extensions/global_variable_set.rb', line 12 def global_variable_get(var, = {}) if .delete(:match_object) return global_variable_get(global_variable_name(var), ) else if var.is_a? Symbol raise NameError.new("#{var} is not a valid global variable name") unless var.to_s[0..0] == '$' return eval("#{var}") else raise ArgumentError.new("var must be a symbol unless :match_object => true") end end end |
#global_variable_name(var) ⇒ Object
Looks up the name of global variable var, which must be an actual global variable.
global_variable_name($a)
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/qualitysmith_extensions/global_variable_set.rb', line 27 def global_variable_name(var) global_variables.each do |test_var| #if eval(test_var).eql?(var) if eval(test_var).object_id == var.object_id #$stderr.puts "Checking #{test_var}. #{eval(test_var).inspect}" #$stderr.puts " #{$stdout.inspect}" return test_var.to_sym end end raise ArgumentError.new("The given object (#{var.inspect}) (#{var.object_id}) is not a valid global variable") end |
#global_variable_set(var, value, options = {}) ⇒ Object
Sets the global variable var, which can either be a symbol or an actual global variable (use :match_object).
global_variable_set(:$a, 'new')
global_variable_set($a, 'new', :match_object => true)
global_variable_set(:$a, "StringIO.new", :eval_string => true)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/qualitysmith_extensions/global_variable_set.rb', line 43 def global_variable_set(var, value, = {}) #puts "global_variable_set(#{var}, #{value.inspect}, #{options.inspect}" if .delete(:match_object) return global_variable_set(global_variable_name(var), value, ) else if var.is_a? Symbol raise NameError.new("#{var} is not a valid global variable name") unless var.to_s[0..0] == '$' if .delete(:eval_string) #puts("About to eval: #{var} = #{value}") eval("#{var} = #{value}") else marshalled_data = Marshal.dump(value) eval("#{var} = Marshal.load(%Q<#{marshalled_data}>)") end return var else raise ArgumentError.new("var must be a symbol unless :match_object => true") end end end |
#pretty_backtrace ⇒ Object
Returns a human-readable backtrace
19 20 21 22 |
# File 'lib/qualitysmith_extensions/kernel/backtrace.rb', line 19 def pretty_backtrace "Backtrace:\n" + backtrace[1..-1].map{|frame| "* " + frame}.join("\n") end |
#require_all(base_dir, options = {}) ⇒ Object
Recursively requires all Ruby files in base_dir or any of its subdirectories. :exclude: An array of regular expressions or glob patterns that will be passed to FileList#exclude. If you specify this option, a file will not be included if it matches any of these patterns. :exclude_files: An array of filenames to exclude. These will be matched exactly, so if you tell it to exclude ‘bar.rb’, ‘foobar.rb’ will not be excluded.
Note:
Examples:
require_all 'lib/', :exclude => [/ignore/, /bogus/] # will require 'lib/a.rb', 'lib/long/path/b.rb', but not 'lib/ignore/c.rb'
require_all File.dirname(__FILE__), :exclude_files => ['blow_up_stuff.rb']
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/qualitysmith_extensions/kernel/require_all.rb', line 25 def require_all(base_dir, = {}) base_dir += '/' unless base_dir[-1].chr == '/' exclusions = nil files = FileList[base_dir + "**/*.rb"] files = files.exclude(*exclusions) if (exclusions = .delete(:exclude)) files = files.exclude(*exclusions.map {|a| File.exact_match_regexp(a) }) if (exclusions = .delete(:exclude_files)) files.each do |filename| # puts "requiring #{filename}" if filename =~ /test/ require filename end end |
#require_once(name) ⇒ Object
Fixes bug in Ruby (1.8, at least – not sure if 2.0 fixes it) where a file can be required twice if the path is spelled differently.
12 13 14 15 16 |
# File 'lib/qualitysmith_extensions/kernel/require_once.rb', line 12 def require_once(name) raise NotImplementedError # store expand_path(name) in an array ($required_files or something) # only do the require if it wasn't already in the array end |
#trap_chain(signal_name, *args, &block) ⇒ Object
Calling Kernel#trap() by itself will replace any previously registered handler code. Kernel#trap_chain(), on the other hand, will add the block you supply to the existing “list” of registered handler blocks. Similar to the way Kernel#at_exit() works, Kernel#trap_chain() will prepend the given block to the call chain for the given signal_name. When the signal occurs, your block will be executed first and then the previously registered handler will be invoked. This can be called repeatedly to create a “chain” of handlers.
13 14 15 16 17 18 19 |
# File 'lib/qualitysmith_extensions/kernel/trap_chain.rb', line 13 def trap_chain(signal_name, *args, &block) previous_interrupt_handler = trap(signal_name, *args) {} trap(signal_name, *args) do block.call previous_interrupt_handler.call unless previous_interrupt_handler == "DEFAULT" end end |