Monday, March 06, 2017

puts_debuggerer v0.5.0 adds caller, formatter, and more goodies

puts_debuggerer v0.5.0

Yes, many of us avoid debuggers like the plague and clamp on to our puts statements like an umbrella in a stormy day. Why not make it official and have puts debugging become its own perfectly legitimate thing?!!
And thus, puts_debuggerer was born. A guilt-free puts debugger Ruby gem FTW!
In other words, puts_debuggerer is a Ruby library for improved puts debugging, automatically displaying bonus useful information such as source line number and source code.
Partially inspired (only partially ;) by this blog post: https://tenderlovemaking.com/2016/02/05/i-am-a-puts-debuggerer.html (Credit to Tenderlove.)
Below are some of its options.

Options

Options enable more data to be displayed with puts_debuggerer, such as the caller backtrace, header, and footer. They also allow customization of output format.

PutsDebuggerer.print_engine

(default = :p)
Print engine to use in object printout (e.g. pappp). It is represented by the print engine's global method name as a symbol (e.g. :ap for awesome_print). Defaults to Ruby's built-in p method identified by the symbol :p. If it finds awesome_print loaded, it defaults to ap as :ap instead.
Example:
# File Name: /Users/User/example.rb
require 'awesome_print'
PutsDebuggerer.print_engine = :ap
array = [1, [2, 3]]
pd array
Prints out:
[PD] /Users/User/example.rb:5
   > pd array
  => [
    [0] 1,
    [1] [
        [0] 2,
        [1] 3
    ]
]

PutsDebuggerer.announcer

(default = "[PD]")
Announcer (e.g. [PD]) to announce every print out with (default: "[PD]")
Example:
PutsDebuggerer.announcer = "*** PD ***\n  "
pd (x=1)
Prints out:
*** PD ***
   /Users/User/example.rb:2
   > pd x=1
  => "1"

PutsDebuggerer.formatter

(default = PutsDebuggerer::FORMATTER_DEFAULT)
Formatter used in every print out Passed a data argument with the following keys:
  • :announcer (string)
  • :caller (array)
  • :file (string)
  • :footer (string)
  • :header (string)
  • :line_number (string)
  • :pd_expression (string)
  • :object (object)
  • :object_printer (proc)
NOTE: data for :object_printer is not a string, yet a proc that must be called to output value. It is a proc as it automatically handles usage of print_engine and encapsulates its details. In any case, data for :object is available should one want to avoid altogether.
Example:
PutsDebuggerer.formatter = -> (data) {
  puts "-<#{data[:announcer]}>-"
  puts "HEADER: #{data[:header]}"
  puts "FILE: #{data[:file]}"
  puts "LINE: #{data[:line_number]}"
  puts "EXPRESSION: #{data[:pd_expression]}"
  print "PRINT OUT: "
  data[:object_printer].call
  puts "CALLER: #{data[:caller].to_a.first}"
  puts "FOOTER: #{data[:footer]}"
}
pd (x=1)
Prints out:
-<[PD]>-
FILE: /Users/User/example.rb
HEADER: ********************************************************************************
LINE: 9
EXPRESSION: x=1
PRINT OUT: 1
CALLER: #/Users/User/master_examples.rb:83:in `block (3 levels) in '
FOOTER: ********************************************************************************

PutsDebuggerer.caller

(default = nil)
Caller backtrace included at the end of every print out Passed an argument of true/false, nil, or depth as an integer.
  • true and -1 means include full caller backtrace
  • false and nil means do not include caller backtrace
  • depth (0-based) means include limited caller backtrace depth
Example:
# File Name: /Users/User/sample_app/lib/sample.rb
PutsDebuggerer.caller = 3
pd (x=1)
Prints out:
[PD] /Users/User/sample_app/lib/sample.rb:3
    > pd x=1
   => "1"
     /Users/User/sample_app/lib/master_samples.rb:368:in \`block (3 levels) in <top (required)>\'
     /Users/User/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/irb/workspace.rb:87:in \`eval\'
     /Users/User/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/irb/workspace.rb:87:in \`evaluate\'
     /Users/User/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/irb/context.rb:381:in \`evaluate\'

Options can be set as a global configuration or piecemeal per puts statement.

Global configuration is done via PutsDebuggerer module attribute writers. On the other hand, piecemeal options can be passed to the pd global method as the second argument.


Example:
# File Name: /Users/User/project/piecemeal.rb
data = [1, [2, 3]]
pd data, header: '>'*80, footer: '<'*80, announcer: "   -<[PD]>-\n  "
Prints out:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
   -<[PD]>-
   /Users/User/project/piecemeal.rb:3
   > pd data
  => [1, [2, 3]]
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Become a puts_debuggerer!

No comments: