Ruby Output
Printing values is the first tool you use to see what a program is doing. Ruby gives you four everyday ways to print: puts, print, p and pp. They look similar but behave differently, and knowing the difference saves a lot of debugging time.
puts — print with a newline
puts converts its argument to a string with to_s, prints it and adds a newline (unless the text already ends with one):
puts "First line"
puts "Second line"
puts 42
puts 3.5
puts
puts "After an empty line"First line
Second line
42
3.5
After an empty line
puts with no argument prints an empty line. Given several arguments — or an array — it prints each element on its own line:
puts "a", "b"
puts [1, 2, 3]
puts [nil, true]a
b
1
2
3
true
Notice that nil becomes an empty line: nil.to_s is the empty string "".
print — no newline
print writes its arguments exactly as they are, without adding a newline. Use "\n" when you want one:
print "Loading"
print "."
print "."
print ".\n"
print "x = ", 10, "\n"Loading...
x = 10
p — inspect for debugging
p prints the inspect form of a value: the representation a programmer would type. Strings keep their quotes, nil is shown as nil, and arrays keep their brackets. That is why p is the go-to tool for debugging.
puts "hello"
p "hello"
puts nil
p nil
puts [1, "two", :three]
p [1, "two", :three]
p({name: "Ada", "role" => "admin"})hello
"hello"
nil
1
two
three
[1, "two", :three]
{name: "Ada", "role" => "admin"}
A second difference: puts and print return nil, while p returns its argument. You can insert p into the middle of an expression to peek at a value without changing the result.
Note: Ruby 3.4 prints hashes as
{name: "Ada", "role" => "admin"}— symbol keys use the shortkey: valueform and other keys keep=>. Older versions printed{:name=>"Ada", "role"=>"admin"}.
pp — pretty print
pp works like p but breaks large nested structures over several lines so they stay readable. For short values it prints exactly the same as p:
order = {
id: 1042,
customer: {name: "Kai Moreno", email: "kai@example.com", city: "Valencia"},
items: [{sku: "TEA-01", qty: 2}, {sku: "MUG-07", qty: 1}],
}
pp order
pp [1, 2, 3]{id: 1042,
customer: {name: "Kai Moreno", email: "kai@example.com", city: "Valencia"},
items: [{sku: "TEA-01", qty: 2}, {sku: "MUG-07", qty: 1}]}
[1, 2, 3]
Comparison
| Method | Conversion | Newline | Returns | Best for |
|---|---|---|---|---|
puts |
to_s |
yes, one per item | nil |
Normal user-facing output |
print |
to_s |
no | nil |
Building a line piece by piece |
p |
inspect |
yes | its argument | Quick debugging |
pp |
inspect (pretty) |
yes | its argument | Debugging big hashes/arrays |
Escape sequences
Inside double-quoted strings, a backslash starts an escape sequence:
puts "Name:\tAda"
puts "Line 1\nLine 2"
puts "She said \"hi\""
puts 'Single quotes keep \n as-is'Name: Ada
Line 1
Line 2
She said "hi"
Single quotes keep \n as-is
| Escape | Meaning |
|---|---|
\n |
newline |
\t |
tab |
\" |
double quote |
\\ |
backslash |
é |
Unicode character (é) |
Tip: Writing
$stdout.puts "x"is the same asputs "x". To print error messages, usewarn "message"— it writes to standard error instead of standard output.
Test yourself
What does p "hi" print?
- hi
- "hi"
- :hi
- nil
Which method prints without adding a newline?
putspprintpp
What does puts [1, 2] print?
- [1, 2]
- 1 2 on one line
- 1 and 2 on separate lines
- 12