January 2024
Intermediate to advanced
718 pages
20h 15m
English
If you want to debug code and you don’t want to use any fancy tools, then printing things out to the console is the way to go. We don’t mean to make fun—we extensively use this method. It’s quick and lends itself to faster cycle times than using a debugger to step through code.
We’ve seen a few options for this already, like puts, which uses to_s to convert its argument to a string, and p, which does the same thing but uses inspect. Ruby also provides print and pretty-print via the pp method. Here’s a table of what they all look like for nil, a string, a symbol, an array, a hash, and an array with a hash element:
| p | puts | pp | |
|---|---|---|---|
|
nil |
nil | ||
|
test |
"test" |
test |
"test" |
|
test |
:test |
test |
:test |
|
[1, 2, 3] |
[1, 2, 3] |
1 |
Read now
Unlock full access