Skip to content
Poignant Guide Ruby Q&A

What is the difference between puts, print and p in Ruby?

Staff answerWritten by Odile Marchetti · Ruby syntax and basics0 replies

Short answer

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.

Written by Odile Marchetti · Staff: Ruby basics, objects and the Poignant Guide ·
CitePoignant 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.
  1. Staff: 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 p shows the quote marks, because a quote mark is part of how Ruby writes a string. That is the whole idea: puts and print are for output a person reads, and p is 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 p as running $stdout.write(obj.inspect, "\n") for each object. $stdout is the place your program's normal output goes, usually the terminal. The same page says print is the straightforward way to write to $stdout and that it converts any object that is not a string with to_s.

    Why they behave differently

    Two conversions explain most of it. to_s turns an object into friendly text, and inspect turns 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 puts takes an array apart and prints each item on its own line, while p prints the array as one thing. The IO documentation describes IO#puts as writing lines and making sure each line ends with a newline. That is why puts "line\n" does not leave an extra blank line.

    What each one returns

    Every Ruby method hands something back. This matters when you put p in 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 p returns the object it was given, or an array of the objects if you pass several, or nil if you pass none. That makes p safe to drop into a line to peek at a value, for example total = p(price * 3). Do the same with puts and total becomes nil, which causes trouble later.

    Which should I use?

    Method New line at the end Converts with Returns Best for
    puts Yes to_s nil Messages for the person running the program
    print No to_s nil Prompts, progress dots, building one line in pieces
    p Yes inspect The argument Debugging, checking what a value really is

    Mistakes beginners make

    1. Using puts to debug and being fooled. puts "5" and puts 5 look identical. p "5" shows quotes and p 5 does not, so p reveals a string that should have been a number.
    2. Forgetting that print has no new line. Two print calls glue their output together. Add "\n" or use puts.
    3. Expecting puts to show nil. puts nil prints an empty line, so a missing value looks like a blank. p nil prints the word.
    4. Using p's return value by accident. If p is the last line of a method, the method returns the value, which is fine. If puts is last, the method returns nil.

    Check this in order

    1. Is your output missing a line break? You probably used print.
    2. Do you see a blank line where you expected a value? Try p on the variable and look for nil.
    3. Does a number behave like text, or text like a number? Compare p x with p x.class.
    4. Did a method return nil after you added a debug line? Check whether you ended it with puts.

    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.

    2
    ReplyLink

Plain text or simple Markdown. Links are nofollow. Your email is never shown.

Poignant Guide Ruby Q&A. Questions and answers on learning Ruby and programming: syntax, blocks, objects, gems, Rails, debugging and reading the Poignant Guide.