puts, p, and Running Files

puts vs. p vs. inspect

Ruby has two everyday printing methods, and they behave differently β€” mixing them up is a classic first-week confusion:

  • puts prints a human-readable version and adds a trailing newline. On nil it prints nothing but a blank line. On a string, no quotes show.
  • p prints the .inspect form β€” the DEBUG view, quotes included β€” and returns the value itself, which makes it handy inside a chain while debugging.
puts "Mochi"     # Mochi
p "Mochi"        # "Mochi"  (quotes shown)
puts nil          # (blank line)
p nil             # nil

Reach for puts when a script talks to a human. Reach for p when you're debugging and need to see exactly what a value IS β€” a symbol vs. a string, nil vs. an empty string, etc.