(defaults to awesome_print as its print_engine now)
Ever wished you'd see debug statements only the first time a puts statement executes, and avoid printing 10 or 100 times in a test run? Now you can with PutsDebuggerer.run_at option.
Set condition for when to run as specified by an index, array, or range.
Default value is nil meaning always
Value as an Integer index (1-based) specifies at which run to print once
Value as an Array of indices specifies at which runs to print multiple times
Value as a range specifies at which runs to print multiple times, indefinitely if it ends with ..-1 or ...-1
Can be set globally via PutsDebuggerer.run_at or piecemeal via pd object, run_at: run_at_value
Global usage should be good enough for most cases. When there is a need to track a single expression among several, you may add the option piecemeal, but it expects the same exact object passed to pd for counting.
Examples (global):
PutsDebuggerer.run_at =1
pd (x=1) # prints standard PD output
pd (x=1) # prints nothingPutsDebuggerer.run_at =2
pd (x=1) # prints nothing
pd (x=1) # prints standard PD outputPutsDebuggerer.run_at = [1, 3]
pd (x=1) # prints standard PD output
pd (x=1) # prints nothing
pd (x=1) # prints standard PD output
pd (x=1) # prints nothingPutsDebuggerer.run_at =3..5
pd (x=1) # prints nothing
pd (x=1) # prints nothing
pd (x=1) # prints standard PD output
pd (x=1) # prints standard PD output
pd (x=1) # prints standard PD output
pd (x=1) # prints nothing
pd (x=1) # prints nothingPutsDebuggerer.run_at =3...6
pd (x=1) # prints nothing
pd (x=1) # prints nothing
pd (x=1) # prints standard PD output
pd (x=1) # prints standard PD output
pd (x=1) # prints standard PD output
pd (x=1) # prints nothingPutsDebuggerer.run_at =3..-1
pd (x=1) # prints nothing
pd (x=1) # prints nothing
pd (x=1) # prints standard PD output
pd (x=1) # ... continue printing indefinitely on all subsequent runsPutsDebuggerer.run_at =3...-1
pd (x=1) # prints nothing
pd (x=1) # prints nothing
pd (x=1) # prints standard PD output
pd (x=1) # ... continue printing indefinitely on all subsequent runs
Key Features:
- Glimmer now uses a Ruby Logger in debug mode to provide helpful debugging information
- Glimmer has a smart new Ruby shell script for executing applications
- ***DRUMROLL*** As part of the smart new Ruby shell script, Glimmer now downloads swt.jar automatically for the user platform whether Mac, Windows, or Linux, and whether it's an x86 or x86-64 CPU architecture.
Glimmer - The Original One and Only - is alive and well. A new version has just been released supporting Ruby 2.3.0 (jruby-9.1.12.0) and SWT 4.7.0, with improved glimmer executable and instructions.
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.
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. p, ap, pp). It is represented by the print engine's global method name as a symbol (e.g. :ap forawesome_print). Defaults to Ruby's built-in p method identified by the symbol :p. If it findsawesome_printloaded, it defaults to ap as :ap instead.
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.
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.
Simply invoke global pd method anywhere you'd like to see line number and source code with output. If the argument is a pure string, the print out is simplified by not showing duplicate source.
Quickly locate printed lines using Find feature (e.g. CTRL+F) by looking for:
[PD]
file:line_number
ruby expression.
This gives you the added benefit of easily removing your pd statements later on from the code.
Happy puts_debuggerering!
Example Code:
# /Users/User/finance_calculator_app/pd_test.rb # line 1
bug ='beattle'# line 2
pd "Show me the source of the bug: #{bug}"# line 3
pd 'What line number am I?'# line 4
Example Printout:
[PD] /Users/User/finance_calculator_app/pd_test.rb:3
> ("Show me the source of the bug: #{bug}").inspect
=>"Show me the source of the bug: beattle"
[PD] /Users/User/finance_calculator_app/pd_test.rb:4 "What line number am I?"
PutsDebuggerer.header
Header to include at the top of every print out.
Default value is nil
Value true enables header as '*'*80
Value false, nil, or empty string disables header
Any other string value gets set as a custom header