cute_print
cute_print writes debug output to stderr. Optionally prints the source filename and line number, or the source of the debug statement. Easily inspect the middle of a call chain.
Why?
- You want to print debug output very quickly, and remove it just as quickly.
- You want to easily label the debug output with the source location.
- You want to easily label the debug output with the debug code itself.
- You want to easily debug the middle of a call chain.
- You are debugging a program that is run with stdout redirected.
Links
- This is a rubygem
- The source is on github
- Cucumber-driven documentation is on relishapp
- API docs are at rubydoc.info
Installation
Add this line to your application's Gemfile:
gem "cute_print"
And then execute:
$ bundle
Or install it yourself:
$ gem install cute_print
Usage
Start with:
require "cute_print"
q work like Kernel#p, except that it prints to $stderr instead of $stdout.
q "abc" # "abc"
q [1, 2, 3 + 4] # [1, 2, 7]
By passing a block, you can have the debug source printed along with the value:
i = 1
q {i + 2} # i + 2 is 3
ql will also print the source location:
ql "abc" # foo.rb:12: "abc"
ql {1 + 2} # foo.rb:13: 1 + 2 is 3
When called with no arguments, ql just prints the source location:
ql # foo.rb:14
This is very handy for answering "which branch did it take?," or "did it even get to that method?"
qq pretty-prints its arguments to $stderr:
a = (1..30).to_a
qq a # [1,
# 2,
# ...
# 20,
# 30]
When called with a bock, qq prints the debug source as well:
a = (1..30).to_a
qq {a} # a is [1,
# 2,
# ...
# 20,
# 30]
qql also prints the source location:
a = (1..30).to_a
qq a # foo.rb:12: [1,
# 2,
# ...
# 20,
# 30]
When called with a block, qql prints the debug source as well:
a = (1..30).to_a
qq {a} # foo.rb:12: a is [1,
# 2,
# ...
# 20,
# 30]
tapq inspects the middle of a call chain:
["1", "2"].map(&:to_i).tapq.inject(&:+)
# [1, 2]
tapql also prints the source location:
["1", "2"].map(&:to_i).tapql.inject(&:+)
# bar.rb:12: [1, 2]
tapqq pretty-prints the middle of a call chain:
a = (1..30).to_a
sum = a.tapqq.inject(:+) # [1,
# 2,
# ...
# 20,
# 30]
tapqql also prints the source location:
a = (1..30).to_a
sum = a.tapqq.inject(:+) # foo.rb:12: [1,
# 2,
# ...
# 20,
# 30]
Configuration
To change the output device:
CutePrint.configure do |c|
c.out = File.open('/tmp/debug')
end
Any object that responds to #print will do.
To cause #ql, #qql and #tapql to print the full path rather than just the filename:
CutePrint.configure do |c|
c.location_format = :path
end
The terminal width is detected, if possible, but you can override it:
CutePrint.configure do |c|
c.term_width = 132
end
To set the terminal width back to being detected:
CutePrint.configure do |c|
c.term_width = :detect
end
To reset the configuration to its defaults:
CutePrint.configure do |c|
c.reset
end
Without modifying Ruby classes
By default, this gem defines many methods on Object, making them globally available. To use this gem without any global methods, require "cute_print/core" and then call one of the CutePrint class methods:
require "cute_print/core"
CutePrint.q {1 + 2} # 1 + 2 is 3
The "tap" methods are not available when using CutePrint this way.
Ruby versions
As of version 1.4.0, cute_print is known to work with these versions of Ruby:
- ruby-2.7.0
- ruby-2.6.6
- ruby-2.5.7
As of version 1.3.0, cute_print is known to work with these versions of Ruby:
- ruby-2.5.7
- ruby-2.4.9
As of version 1.1.6, cute_print is known to work with these versions of Ruby:
- ruby-2.5.0
- ruby-2.4.3
- ruby-2.3.6
- ruby-2.2.9
As of version 1.1.5, cute_print is known to work with these versin of Ruby:
- ruby-2.1.10
- ruby-1.9.3 (tests pass but with warnings)
Platforms supported
This gem is developed and tested with Linux. It should work on Windows and OSX as well. If it does not work for you, please file an issue on github.
Other gems with similar functions
The wrong gem includes the excellent #d method, which is very much like this gem's #q method. This gem's ability to read the debug statement's source is derived from the wrong gem.
Versioning
This gem uses semantic versioning 2.0.
Contributing
- Fork it ( http://github.com/wconrad/cute_print/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request