How to Print Output in Ruby with puts
puts is how a Ruby script writes to the terminal: it prints a value and ends the line. Two relatives fill the gaps: print doesn't end the line, and p shows a value the way Ruby sees it, quotes and all.
Syntax
puts <value>
print <value>
p <value>
| Part | What it does |
|---|---|
puts | Prints the value and a new line; each item of an array goes on its own line. |
print | Prints the value without a new line. |
p | Prints the value's inspect form, such as "text" with quotes, and returns the value. |
The Ruby documentation for Kernel describes puts as "Equivalent to $stdout.puts(*objects) for the given objects", and notes that p "is designed for debugging purposes".
The Example
The example prints a sentence, a number and an array with puts, builds one line from print and puts, and prints a string with p.
Prerequisites
- Ruby. This guide uses Ruby 4.0.10.
Step 1: Write the Code
Create a file named main.rb:

puts 'Hello from Ruby'
puts 42
puts ['a', 'b']
print 'No new line, '
puts 'then a new line.'
p 'with quotes'
Step 2: Run It
ruby main.rb
Hello from Ruby
42
a
b
No new line, then a new line.
"with quotes"

puts ['a', 'b']printed each item on its own line.printleft the line open for the nextputs.pkept the quotes, which shows that the value is a string.
The command is the same in zsh, bash and PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using puts to debug a value | puts nil prints an empty line, and puts '' looks the same | Use p, which shows nil and "" |
Using print for every line | Everything runs together on one line | Use puts, or add "\n" |
Expecting puts to return what it printed | It returns nil | Use p when you need the value back |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| An empty line where a value should be | The value is nil or '' | Print it with p to see which |
| Output runs together | print doesn't end the line | Switch to puts |
FAQ
Where does puts write?
To standard output. For error messages, warn 'message' writes to standard error instead.
Why does p return its argument?
So you can wrap it around an expression while debugging without changing what the code does: x = p 'a' printed "a" and set x to "a".
Conclusion
puts prints a value and a new line, print leaves the line open, and p shows the value's inspected form for debugging.
Additional Resources
- Kernel#puts, in the Ruby documentation.
- Kernel#p, in the Ruby documentation.
