puts, print, and p
puts, print, and p are three ways to show things on screen: puts adds a new line, print stays on the same line, and p shows the value the way a programmer sees it. They are like three different ways of speaking out loud.
In this page:
Syntax
puts value
print value
p value
puts, print, and p
puts prints a value followed by a newline, print prints without adding one, and p prints the value's inspected (debug) representation, including quotes around strings.
p is especially useful while debugging since it makes the type of the value clearer than puts. All three accept multiple arguments.
Note:
Use
p instead of puts when debugging, since it shows exactly what type of value you have, quotes and all.Example: puts, print, and p
puts "Hello with puts"
print "Hello with print"
print "\n"
p "Hello with p"
p [1, 2, 3]
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Using
putson an array and being surprised that each item is on its own line, whenpshows the array with brackets. - Expecting
print "Hello"to end the line, when it adds no newline. - Using
pfor output shown to users, when it prints the inspect format with quotes.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: