Bird
Raised Fist0
Rubyprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main difference between puts and print in Ruby?
easy
A. puts adds a new line after output, print does not.
B. print adds a new line after output, puts does not.
C. puts outputs the object's inspect form, print outputs the string form.
D. print outputs the object's inspect form, puts outputs the string form.

Solution

  1. Step 1: Understand puts behavior

    puts prints the given value and adds a new line automatically after it.
  2. Step 2: Understand print behavior

    print prints the given value but does not add a new line after it.
  3. Final Answer:

    puts adds a new line after output, print does not. -> Option A
  4. Quick Check:

    puts adds newline = C [OK]
Hint: Remember: puts adds newline, print stays on same line [OK]
Common Mistakes:
  • Confusing which method adds a newline
  • Thinking print adds newline
  • Mixing up output formats of puts and print
2. Which of the following is the correct syntax to print the string "Hello" without a newline in Ruby?
easy
A. puts "Hello"
B. print "Hello"
C. p "Hello"
D. puts(Hello)

Solution

  1. Step 1: Identify method to print without newline

    print prints without adding a newline.
  2. Step 2: Check syntax correctness

    print "Hello" is correct syntax; puts adds newline, p prints inspect form, and puts(Hello) is invalid because Hello is not a string.
  3. Final Answer:

    print "Hello" -> Option B
  4. Quick Check:

    print without newline = A [OK]
Hint: Use print for no newline, puts for newline [OK]
Common Mistakes:
  • Using puts when newline is not wanted
  • Forgetting quotes around strings
  • Using p instead of print or puts
3. What will be the output of the following Ruby code?
puts "Hi"
print "There"
p "!"
medium
A. Hi There! "!"
B. Hi There"!"
C. "!"erehT iH
D. Hi There "!"

Solution

  1. Step 1: Analyze puts "Hi"

    This prints "Hi" followed by a newline.
  2. Step 2: Analyze print "There"

    This prints "There" without a newline, so the cursor stays on the same line.
  3. Step 3: Analyze p "!"

    p prints the inspect form of the string, which includes quotes, and adds a newline. So it prints "!" with quotes and a newline.
  4. Final Answer:

    Hi There "!" -> Option D
  5. Quick Check:

    puts adds newline, print no newline, p shows quotes = A [OK]
Hint: Remember p shows quotes and adds newline [OK]
Common Mistakes:
  • Ignoring quotes printed by p
  • Assuming print adds newline
  • Mixing output lines
4. The following Ruby code is intended to print the array ["1", "2", "3"] with quotes and a newline, but it does not work as expected:
puts ["1", "2", "3"]
What is the best fix?
medium
A. Replace puts with print
B. Add .to_s to the array
C. Replace puts with p
D. Use puts ["1", "2", "3"].inspect

Solution

  1. Step 1: Understand puts ["1", "2", "3"] output

    puts prints each element on a new line without quotes, so output is: 1 2 3
  2. Step 2: Use p to print with quotes and inspect form

    p ["1", "2", "3"] prints the array as it is, including brackets and quotes if strings, with a newline.
  3. Final Answer:

    Replace puts with p -> Option C
  4. Quick Check:

    p prints inspect form with quotes = B [OK]
Hint: Use p to see exact object form with quotes [OK]
Common Mistakes:
  • Using puts expecting quotes
  • Using print which prints without newline
  • Adding to_s which loses inspect format
5. You want to print a nested array in Ruby so that each element is shown with quotes and the entire structure is visible on one line. Which code snippet achieves this?
hard
A. p [["1", "2"], ["3", "4"]]
B. print [["1", "2"], ["3", "4"]]
C. puts [["1", "2"], ["3", "4"]].to_s
D. puts [["1", "2"], ["3", "4"]]

Solution

  1. Step 1: Analyze puts [["1", "2"], ["3", "4"]]

    This prints each inner array on a separate line without quotes, so output is: [1, 2] [3, 4]
  2. Step 2: Analyze print [["1", "2"], ["3", "4"]]

    This prints the array on one line but without quotes around elements, so output is: [[1, 2], [3, 4]] (no quotes)
  3. Step 3: Analyze p [["1", "2"], ["3", "4"]]

    p prints the inspect form, showing quotes if strings and the full structure on one line, exactly as the array looks.
  4. Step 4: Analyze puts [["1", "2"], ["3", "4"]].to_s

    This converts the array to string and prints it with a newline, but inner elements are not quoted as inspect would show.
  5. Final Answer:

    p [["1", "2"], ["3", "4"]] -> Option A
  6. Quick Check:

    p prints full inspect form on one line = D [OK]
Hint: Use p to print full structure with quotes on one line [OK]
Common Mistakes:
  • Using puts which prints each element on separate lines
  • Using print which lacks quotes
  • Using to_s which loses inspect formatting
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard