Debugging
p and pp
p prints the inspect form and returns the value, so you can wrap any expression without breaking the code around it.
pp pretty prints nested arrays and hashes across lines. Require the pp library first, while p needs no require. The difference between friendly and programmer output is covered in
Duck typing and core idioms.
require "pp" data = { name: "Ada", age: 36, languages: ["ruby", "python"], address: { city: "London", zip: "N1" } } p data pp data
ruby pp_demo.rb
{name: "Ada", age: 36, languages: ["ruby", "python"], address: {city: "London", zip: "N1"}}
{name: "Ada",
age: 36,
languages: ["ruby", "python"],
address: {city: "London", zip: "N1"}}
Warn to stderr
warn prints to the error stream instead of normal output. Warnings stay visible even when normal output goes to a file.
Use it for deprecation notes and retries, and keep puts for real program output.
warn "Retrying in 5 seconds" puts "Continuing"
ruby warn_demo.rb
Continuing
The warning line, Retrying in 5 seconds, went to the error stream, so a capture of the standard output holds only the second line.
The debug gem basics
The debug gem adds breakpoints and stepping to plain Ruby. Require it, place binding dot break where you want to pause, and run the script normally.
At the prompt, type n for next line, c to continue, and p plus a name to inspect a variable.
require "debug" total = [1, 2, 3].sum binding.break puts total
Install the gem once
gem install debug
1 gem installed
Save the file as dbg.rb and run it
ruby dbg.rb
[1, 5] in dbg.rb 1| require "debug" 2| 3| total = [1, 2, 3].sum => 4| binding.break 5| puts total =>#0 <main> at dbg.rb:4 (rdbg) p total => 6 (rdbg) continue 6
The arrow marks the line that is about to run. p total prints the value right there, continue lets the program finish, and the last line is the output of puts.
Halve the code
When the bug hides in a large file, disable half the code and rerun. If the bug stays, it lives in the active half.
Repeat until the guilty lines remain. Wrapping suspects in a branch behind a flag set to false is a quick switch for this game.
enabled = false if enabled puts "suspect code here" end
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
preturn?Answer
The value it printed
It returns its argument, so it can be wrapped around any expression without breaking the code.
-
What does this program print?
x = p(1 + 2) p xAnswer
3 / 3
p prints 3 and returns it, so x is 3 and the second p prints it again.
-
What does this program print?
p({ a: 1 })Answer
{a: 1}
p uses inspect, and Ruby 3.4 prints a symbol key in the short form.
-
What does this program print to the standard output?
warn "Retrying in 5 seconds" puts "Continuing"Answer
Continuing
warn writes to the error stream, so the standard output holds only the puts line.
-
Where does
warnwrite its line?Answer
The error stream
That is why a warning stays visible when the normal output is redirected to a file.
-
At a
binding.breakprompt, which key steps to the next line and which one continues?Answer
n steps, c continues
n is next, c is continue, and p plus a name inspects a variable.
-
What does this program print?
enabled = false if enabled puts "suspect code here" end puts "done"Answer
done
The disabled branch prints nothing, and the line after it still runs.
-
What does
pshow thatputsdoes not?Answer
The inspect form, with quotes and structure
puts calls to_s, p calls inspect, which is the form a programmer reads.
-
What does the
debuggem need beforebinding.breakcan pause a script?Answer
The gem required at the top of the file
require "debug" loads the breakpoint support, and then the script pauses at the call.
-
Half the code is disabled and the bug disappears. Where is the bug?
Answer
In the disabled half
Halving keeps the half that still shows the bug, so the bug is where the behaviour changes.
Article author: Arthur Isaev