What is the difference between puts, print and p in Ruby?
puts prints a value and then starts a new line, print prints it with no new line, and p prints the inspect form, which shows the object as Ruby sees it, with quotes around strings. puts and print return nil, while p returns its argument. Use puts for output people read and p for looking inside.
Cite
Poignant Guide Ruby Q&A, "What is the difference between puts, print and p in Ruby?", https://poignantguide.net/answers/read/what-is-the-difference-between-puts-print-and-p-in-ruby/, accessed 2026-11-15.- Odile MarchettiStaffStaff: Ruby basics, objects and the Poignant Guide ·
The same value, three ways
Save this as
show.rb(or paste it into irb) and run it:name = "Ada" puts name # => Ada print name # => Ada (no new line, so the next output starts right after it) print "\n" p name # => "Ada"All three show the letters A, d, a. Only
pshows the quote marks, because a quote mark is part of how Ruby writes a string. That is the whole idea:putsandprintare for output a person reads, andpis for output a programmer reads.A method is a named piece of Ruby code you call, and these three are methods from Kernel, the module Ruby makes available everywhere. The Kernel documentation describes
pas running$stdout.write(obj.inspect, "\n")for each object.$stdoutis the place your program's normal output goes, usually the terminal. The same page saysprintis the straightforward way to write to$stdoutand that it converts any object that is not a string withto_s.Why they behave differently
Two conversions explain most of it.
to_sturns an object into friendly text, andinspectturns it into a developer-friendly description. Compare:puts nil # prints an empty line p nil # => nil puts [1, [2, 3]] # => 1 # => 2 # => 3 p [1, [2, 3]] # => [1, [2, 3]] puts :sym # => sym p :sym # => :sym puts "a\tb" # => a b (a real tab) p "a\tb" # => "a\tb"So
putstakes an array apart and prints each item on its own line, whilepprints the array as one thing. The IO documentation describesIO#putsas writing lines and making sure each line ends with a newline. That is whyputs "line\n"does not leave an extra blank line.What each one returns
Every Ruby method hands something back. This matters when you put
pin the middle of working code:a = puts("x") # prints x p a # => nil b = p("hi") # prints "hi" p b # => "hi"The Kernel page says
preturns the object it was given, or an array of the objects if you pass several, ornilif you pass none. That makespsafe to drop into a line to peek at a value, for exampletotal = p(price * 3). Do the same withputsandtotalbecomesnil, which causes trouble later.Which should I use?
Method New line at the end Converts with Returns Best for putsYes to_snilMessages for the person running the program printNo to_snilPrompts, progress dots, building one line in pieces pYes inspectThe argument Debugging, checking what a value really is Mistakes beginners make
- Using puts to debug and being fooled.
puts "5"andputs 5look identical.p "5"shows quotes andp 5does not, sopreveals a string that should have been a number. - Forgetting that print has no new line. Two
printcalls glue their output together. Add"\n"or useputs. - Expecting puts to show nil.
puts nilprints an empty line, so a missing value looks like a blank.p nilprints the word. - Using p's return value by accident. If
pis the last line of a method, the method returns the value, which is fine. Ifputsis last, the method returnsnil.
Check this in order
- Is your output missing a line break? You probably used
print. - Do you see a blank line where you expected a value? Try
pon the variable and look fornil. - Does a number behave like text, or text like a number? Compare
p xwithp x.class. - Did a method return
nilafter you added a debug line? Check whether you ended it withputs.
You can practice all three in irb; the guide's Expansion Pak 1 shows how to start it, and Chapter 3 gives you plenty of small lines to try. If irb is not on your machine yet, see How do I install Ruby on a Mac with Homebrew and rbenv? first.
Checked on 12 November 2026.
- Using puts to debug and being fooled.
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.