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
✗ Incorrect
puts adds a new line after printing the output. p also adds a new line because it outputs the inspected object followed by a new line.
What does p "hello" output?
Ahello\n
Bhello
C"hello"
Dhello with no quotes
✗ Incorrect
p shows the string with quotes, so it outputs "hello".
Which method prints without adding a new line?
Aputs
BAll add new lines
Cp
Dprint
✗ Incorrect
print outputs without adding a new line.
Which method is best for debugging to see object details?
Ap
Bprint
Cputs
DNone
✗ Incorrect
p shows the internal structure of objects, useful for debugging.
What will puts [1, 2, 3] output?
A[1, 2, 3]
B1\n2\n3
C1 2 3
DError
✗ Incorrect
puts prints each element of the array on its own line.
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.
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
Step 1: Understand puts behavior
puts prints the given value and adds a new line automatically after it.
Step 2: Understand print behavior
print prints the given value but does not add a new line after it.
Final Answer:
puts adds a new line after output, print does not. -> Option A
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
Step 1: Identify method to print without newline
print prints without adding a newline.
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.
Final Answer:
print "Hello" -> Option B
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
Step 1: Analyze puts "Hi"
This prints "Hi" followed by a newline.
Step 2: Analyze print "There"
This prints "There" without a newline, so the cursor stays on the same line.
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.
Final Answer:
Hi
There
"!"
-> Option D
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
Step 1: Understand puts ["1", "2", "3"] output
puts prints each element on a new line without quotes, so output is:
1
2
3
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.
Final Answer:
Replace puts with p -> Option C
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
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]
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)
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.