Created
May 28, 2010 17:13
-
-
Save MadBomber/417429 to your computer and use it in GitHub Desktop.
A tool to print the labeled value of variables edi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################## | |
## debug_me.rb | |
## A tool to print the labeled value of variables. | |
## Works with local, instance and class variables. | |
## Example usage: | |
=begin | |
debug_me # Prints only the header banner consisting of tag, method name, file name and line number | |
debug_me('INFO') # Also prints only the header but with a different tag | |
debug_me {} # prints the default header and __ALL__ variables | |
debug_me {:just_this_variable} # prints the default header and the value of only one specific variable | |
debug_me { [:this_one, :that_one, :that_other_one] } # prints default header and three specific variables | |
debug_me(:header => false) {} # disables the printing of the header; prints all variables | |
debug_me(:tag => 'MyTag', :header => false) {} # disables header, sets different tag, prints all variables | |
debug_me('=== LOOK ===') {} # changes the tag and prints all variables with a header line | |
debug_me('=== LOOK ===') {:@foo} # changes the tag, prints a header line and a specific instance variable | |
debug_me('=== LOOK ===') {:@@foo} # changes the tag, prints a header line and a specific class variable | |
debug_me(:ivar => false, :cvar => false) {} # print only the local variables with the default tag and a header line | |
=end | |
def debug_me( options={}, &block ) | |
default_options = { :tag => 'DEBUG:', # A tag to prepend to each output line | |
:time => true, # Include a time-stamp in front of the tag | |
:header => true, # Print a header string before printing the variables | |
:ivar => true, # Include instance variables in the output | |
:cvar => true, # Include class variables in the output | |
:file => $stdout # The output file | |
} | |
if 'Hash' == options.class.to_s | |
options = default_options.merge(options) | |
else | |
options = default_options.merge({:tag => options}) | |
end | |
f = options[:file] | |
s = "" | |
s += "#{sprintf('%010.6f', Time.now.to_f)} " if options[:time] | |
s += " #{options[:tag]}" | |
wf = caller # where_from under 1.8.6 its a stack trace array under 1.8.7 is a string | |
wf = wf[0] if 'Array' == wf.class.to_s | |
f.puts "#{s} Source: #{wf}" if options[:header] | |
if block_given? | |
block_value = [ block.call ].flatten.compact | |
if block_value.empty? | |
block_value = eval('local_variables', block.binding) | |
block_value += [ eval('instance_variables', block.binding) ] if options[:ivar] | |
block_value += [ self.class.send('class_variables') ] if options[:cvar] | |
block_value = block_value.flatten.compact | |
else | |
block_value.map! { |v| v.to_s } | |
end | |
block_value.each do |v| | |
ev = eval(v, block.binding) | |
f.puts "#{s} #{v} -=> #{ev.pretty_inspect}" | |
end | |
end ## if block_given? | |
f.flush | |
end ## end of def debug_me | |
def log_me(msg, opts={}) | |
debug_me({:tag => msg, :header => false}.merge(opts)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment