0
0
Rubyprogramming~10 mins

Puts, print, and p output differences in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Puts, print, and p output differences
Start
Call puts
Prints string + newline
Call print
Prints string without newline
Call p
Prints inspected string + newline
End
The program calls puts, print, and p in order, each printing output differently: puts adds a newline, print does not, and p shows the inspected value with newline.
Execution Sample
Ruby
puts "Hello"
print "Hello"
p "Hello"
This code prints the string "Hello" three times using puts, print, and p to show their output differences.
Execution Table
StepMethod CalledOutputNewline Added?Output Description
1puts "Hello"Hello YesPrints string with newline
2print "Hello"HelloNoPrints string without newline
3p "Hello""Hello" YesPrints inspected string with newline
💡 All three methods executed, demonstrating output differences.
Variable Tracker
VariableStartAfter putsAfter printAfter pFinal
output_stream"""Hello\n""Hello\nHello""Hello\nHello\"Hello\"\n""Hello\nHello\"Hello\"\n"
Key Moments - 3 Insights
Why does puts add a newline but print does not?
puts automatically adds a newline after printing, as shown in execution_table step 1, while print outputs exactly what you give it without adding anything extra (step 2).
Why does p show quotes around the string?
p prints the inspected version of the object, which for strings includes quotes, as seen in execution_table step 3.
Does print ever add a newline automatically?
No, print never adds a newline; it only outputs the exact string given, so you must add \n yourself if needed (see step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which method outputs the string with quotes?
Ap
Bputs
Cprint
DNone of them
💡 Hint
Check the Output column in step 3 where quotes appear.
At which step does the output NOT include a newline?
AStep 1
BStep 2
CStep 3
DAll steps include newline
💡 Hint
Look at the Newline Added? column in the execution_table.
If you want to print without a newline, which method should you use?
Aputs
Bp
Cprint
DAll add newline
💡 Hint
Refer to the Newline Added? column for each method.
Concept Snapshot
puts prints string with a newline added automatically
print prints string exactly as is, no newline
p prints inspected object (shows quotes for strings) with newline
Use puts for lines, print for inline output, p for debugging
Full Transcript
This visual execution shows how Ruby's puts, print, and p methods output strings differently. puts adds a newline after printing, print outputs exactly what you give without newline, and p prints the inspected version of the object including quotes for strings, also adding a newline. The execution table traces each method call and its output, while the variable tracker shows how the output stream builds up. Key moments clarify why puts adds newlines, why p shows quotes, and that print never adds newlines automatically. The quiz tests understanding of these differences by referencing the execution table and variable tracker.