Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Ruby Basics  ›  Lesson

puts, print and p

puts, print, p 12 minputs prints readable text with a newline, print adds no newline, and p shows a value's inspected form and returns it, which makes it the tool for debugging.

puts, print and p

At closing time, Bean Cart's script should print a little report. Ruby gives you three everyday ways to write to the screen, and they differ in two small details that matter: whether a newline is added, and whether you see the value as a reader would or as a programmer would.

The rule

puts is for people, print adds no newline, p is for programmers.

puts ("put string") converts each argument to text and ends each one with a newline:

puts "Bean Cart"
puts "Cups sold:", 12
# Bean Cart
# Cups sold:
# 12

Give puts an array and it prints each element on its own line. Give it nil and it prints an empty line. It always returns nil.

print writes the text and adds nothing, so the next output continues on the same line:

print "Latte "
print "Mocha "
print "Tea\n"
# Latte Mocha Tea

p shows the inspected form of a value, the way you would type it in code. Strings keep their quotes, nil shows as nil, and hidden characters become visible. It also returns its argument, which makes it handy for debugging in the middle of an expression:

p "latte"          # "latte"
p nil              # nil
p "tea\n"          # "tea\n"
total = p(4.5 * 2) # prints 9.0, and total is 9.0

Compare with puts "latte", which prints latte with no quotes, and puts nil, which prints only a blank line. When you are hunting a bug, p tells you the truth; puts makes things look nice.

For bigger structures there is also pp ("pretty print"), which spreads nested hashes and arrays over several lines.

The shortcut

Ask "Who is reading this?" A customer or user reads it: puts. You are building one line in pieces: print. You, the programmer, need to see exactly what a value is: p.

Debugging an empty report lineStep 1 of 4
  1. The symptom

    puts item_name prints a blank line. Is item_name an empty string, or nil?

The trap is trusting puts while debugging. puts nil, puts "" and puts [] all print a blank line, so three very different values look the same.

Drills

Answer fast — trust the rule.

quick drill

What does p "mocha" print?

quick drill

Which method writes text without adding a newline at the end?

quick drill

What does puts [1, 2, 3] print?

quick drill

After total = puts 9.0, what is total?

quick drill

A value prints as a blank line with puts. Which tool best shows whether it is nil or an empty string?

Remember

  • puts is for people, print adds no newline, p is for programmers.
  • Ask "who is reading this?" to pick one.
  • Trap: puts hides the difference between nil, "" and []; debug with p.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhich one for debugging?

A variable prints as a blank line with puts. Explain why puts cannot tell you what the value is, which method you would use instead, and what the difference between puts, print and p is.