0
0
Rubyprogramming~5 mins

Puts, print, and p output differences in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does puts do in Ruby?
puts prints the given object to the screen followed by a new line. It converts the object to a string if needed.
Click to reveal answer
beginner
How is print different from puts?
print outputs the given object without adding a new line at the end, so the next output continues on the same line.
Click to reveal answer
intermediate
What is special about p in Ruby?
p prints the object in a way that shows its internal structure, like quotes around strings and array brackets. It is useful for debugging.
Click to reveal answer
beginner
Which method adds a new line automatically after printing?
puts adds a new line after printing. print does not add a new line automatically. p adds a new line because it calls inspect and then outputs with a new line.
Click to reveal answer
intermediate
When should you use p instead of puts or print?
Use p when you want to see the exact representation of an object, including quotes and special characters, which helps in debugging.
Click to reveal answer
Which Ruby method adds a new line after printing?
Aprint
Bputs
Cp
DNone of these
What does p "hello" output?
Ahello\n
Bhello
C"hello"
Dhello with no quotes
Which method prints without adding a new line?
Aputs
BAll add new lines
Cp
Dprint
Which method is best for debugging to see object details?
Ap
Bprint
Cputs
DNone
What will puts [1, 2, 3] output?
A[1, 2, 3]
B1\n2\n3
C1 2 3
DError
Explain the differences between puts, print, and p in Ruby.
Think about how each method handles new lines and object representation.
You got /4 concepts.
    When would you choose to use p instead of puts or print?
    Consider what helps you understand the data better during debugging.
    You got /3 concepts.