betterp
Overview
betterp emulates Ruby's default p with a few extra goodies.
The standard Kernel#p method is overwritten with a version that provides the following features:
- All output is prefixed with the file and line number that made the call to
p - The output prefix is colourised. The colour is selected using a hash of the file path and line number so the colours are stable between each run.
- Profiling: pass a block to
porppto time the block and include the duration in the output.
The original semantics of Kernel#p are still applied, i.e. it returns the value(s) passed to it and the result of #inspect on each argument is output to a separate line.
Installation
Add the gem to your Gemfile
gem 'betterp', '~> 0.1.10'And rebuild your bundle:
$ bundle installOr install standalone:
$ gem install betterp -v '0.1.10'Usage
Call p from anywhere in your code just as you normally would:
require 'betterp'
p 'hello'Profiling
Both p and pp can receive a block. If a block is received, the block will be timed and the execution time will be included in the output. The result of the block is returned to the caller:
require 'betterp'
result = p { fetch_all_the_things }The execution time is highlighted in green, yellow, or red depending on duration. The default thresholds are:
-
low:< 10ms(green) -
high:> 100ms(red)
Durations in between are highlighted yellow. The thresholds (in milliseconds) can be modified:
Betterp.configure do |config|
config.profiling_threshold.low = 100
config.profiling_threshold.high = 1000
endContributing
Bug reports and pull requests are welcome on GitHub at https://github.com/bobf/betterp
