Puts, print, and p output differences in Ruby - Time & Space Complexity
We want to understand how the time it takes to show output changes when using different Ruby commands: puts, print, and p.
How does the way these commands work affect how long the program runs as output grows?
Analyze the time complexity of the following Ruby code snippet.
array = [1, 2, 3, 4, 5]
array.each do |item|
puts item
print item
p item
end
This code prints each item in the array three times using puts, print, and p.
Look at what repeats in the code.
- Primary operation: Looping through each item in the array and printing it three times.
- How many times: Once for each item in the array (n times).
As the array gets bigger, the number of print actions grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 30 (10 items x 3 prints each) |
| 100 | 300 (100 items x 3 prints each) |
| 1000 | 3000 (1000 items x 3 prints each) |
Pattern observation: The total print actions grow directly with the number of items.
Time Complexity: O(n)
This means the time to print grows in a straight line as the number of items grows.
[X] Wrong: "Using p is slower because it does more work, so it changes the time complexity."
[OK] Correct: While p shows extra details, it still prints once per item, so the overall time still grows linearly with input size.
Understanding how output commands scale helps you explain program speed clearly and shows you can think about how code behaves as data grows.
What if we printed each item twice instead of three times? How would the time complexity change?
Practice
puts and print in Ruby?Solution
Step 1: Understand
putsbehaviorputsprints the given value and adds a new line automatically after it.Step 2: Understand
printbehaviorprintprints the given value but does not add a new line after it.Final Answer:
putsadds a new line after output,printdoes not. -> Option AQuick Check:
putsadds newline = C [OK]
- Confusing which method adds a newline
- Thinking print adds newline
- Mixing up output formats of puts and print
Solution
Step 1: Identify method to print without newline
printprints without adding a newline.Step 2: Check syntax correctness
print "Hello"is correct syntax;putsadds newline,pprints inspect form, andputs(Hello)is invalid because Hello is not a string.Final Answer:
print "Hello" -> Option BQuick Check:
print without newline = A [OK]
- Using puts when newline is not wanted
- Forgetting quotes around strings
- Using p instead of print or puts
puts "Hi" print "There" p "!"
Solution
Step 1: Analyze
This prints "Hi" followed by a newline.puts "Hi"Step 2: Analyze
This prints "There" without a newline, so the cursor stays on the same line.print "There"Step 3: Analyze
p "!"pprints 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 DQuick Check:
puts adds newline, print no newline, p shows quotes = A [OK]
- Ignoring quotes printed by p
- Assuming print adds newline
- Mixing output lines
puts ["1", "2", "3"]What is the best fix?
Solution
Step 1: Understand
puts ["1", "2", "3"]outputputsprints each element on a new line without quotes, so output is: 1 2 3Step 2: Use
pto print with quotes and inspect formp ["1", "2", "3"]prints the array as it is, including brackets and quotes if strings, with a newline.Final Answer:
Replaceputswithp-> Option CQuick Check:
p prints inspect form with quotes = B [OK]
- Using puts expecting quotes
- Using print which prints without newline
- Adding to_s which loses inspect format
Solution
Step 1: Analyze
This prints each inner array on a separate line without quotes, so output is: [1, 2] [3, 4]puts [["1", "2"], ["3", "4"]]Step 2: Analyze
This prints the array on one line but without quotes around elements, so output is: [[1, 2], [3, 4]] (no quotes)print [["1", "2"], ["3", "4"]]Step 3: Analyze
p [["1", "2"], ["3", "4"]]pprints the inspect form, showing quotes if strings and the full structure on one line, exactly as the array looks.Step 4: Analyze
This converts the array to string and prints it with a newline, but inner elements are not quoted as inspect would show.puts [["1", "2"], ["3", "4"]].to_sFinal Answer:
p [["1", "2"], ["3", "4"]] -> Option AQuick Check:
p prints full inspect form on one line = D [OK]
- Using puts which prints each element on separate lines
- Using print which lacks quotes
- Using to_s which loses inspect formatting
Ready to prove it?
Pass the test and this pattern moves unknown
