Using the Times Method for Counted Repetition in Ruby
📖 Scenario: You are helping a small bakery keep track of how many cupcakes they bake each day. They want to repeat a simple message for each cupcake baked to motivate their team.
🎯 Goal: Build a Ruby program that uses the times method to repeat a message exactly 5 times, showing the cupcake number each time.
📋 What You'll Learn
Create a variable called cupcakes and set it to 5
Create a variable called message and set it to the string "Cupcake baked!"
Use the times method on cupcakes to repeat a block 5 times
Inside the block, print the message followed by the cupcake number starting from 1
Print the output exactly as shown in the final output
💡 Why This Matters
🌍 Real World
Bakeries and other small businesses often need to repeat tasks or messages a set number of times, like counting items or printing labels.
💼 Career
Understanding counted repetition with methods like <code>times</code> is a basic skill for automating repetitive tasks in programming jobs.
Progress0 / 4 steps
1
Set up the number of cupcakes
Create a variable called cupcakes and set it to the number 5.
Ruby
Hint
Use = to assign the number 5 to the variable cupcakes.
2
Create the message variable
Create a variable called message and set it to the string "Cupcake baked!".
Ruby
Hint
Remember to use quotes around the text to make it a string.
3
Use the times method to repeat the message
Use the times method on cupcakes to repeat a block 5 times. Inside the block, print the message followed by the cupcake number starting from 1. Use puts and the block variable i to show the number.
Ruby
Hint
The block variable i starts at 0, so add 1 to show cupcake numbers starting at 1.
4
Print the final output
Run the program to print the message 5 times with cupcake numbers from 1 to 5.
Ruby
Hint
Make sure your program prints exactly 5 lines, each with the message and the correct cupcake number.
Practice
(1/5)
1. What does the Ruby times method do when called on an integer?
easy
A. Repeats the given block of code that many times starting from zero.
B. Returns an array with that many elements.
C. Creates a range from zero to the integer.
D. Prints the integer value to the screen.
Solution
Step 1: Understand the times method purpose
The times method repeats a block of code a specified number of times, starting from zero.
Step 2: Identify what happens when called on an integer
When called on an integer, it runs the block that many times, passing the current count (starting at zero) to the block.
Final Answer:
Repeats the given block of code that many times starting from zero. -> Option A
Quick Check:
5.times { |i| puts i } prints 0 to 4 [OK]
Hint: Remember: times runs code block count times from zero [OK]
Common Mistakes:
Thinking it returns an array
Assuming it starts counting from 1
Confusing it with range creation
2. Which of the following is the correct syntax to print numbers 0 to 4 using times in Ruby?
easy
A. times(5) { |i| puts i }
B. 5.times do |i| puts i end
C. 5.times { i in puts i }
D. 5.times puts i
Solution
Step 1: Check Ruby times syntax
The correct syntax is calling times on an integer with a block, either with do...end or curly braces.
Step 2: Identify the correct block parameter usage
5.times do |i| puts i end uses 5.times do |i| puts i end, which is valid Ruby syntax to print 0 to 4.
Final Answer:
5.times do |i| puts i end -> Option B
Quick Check:
Correct block syntax = 5.times do |i| puts i end [OK]
Hint: Use integer.times do |i| ... end for correct syntax [OK]
Common Mistakes:
Using times(5) which is invalid
Incorrect block syntax like { i in ... }
Omitting block braces or do/end
3. What will be the output of this Ruby code?
3.times { |i| puts i * 2 }
medium
A. 0
2
4
B. 1
2
3
C. 2
4
6
D. 0
1
2
Solution
Step 1: Understand the iteration count and block variable
The 3.times runs the block 3 times with i values 0, 1, 2.
Step 2: Calculate the output for each iteration
Each iteration prints i * 2: 0*2=0, 1*2=2, 2*2=4.
Final Answer:
0
2
4 -> Option A
Quick Check:
Multiply index by 2 = 0, 2, 4 [OK]
Hint: Multiply block index by 2 to get output [OK]
Common Mistakes:
Assuming index starts at 1
Printing index without multiplication
Confusing output order
4. Identify the error in this Ruby code snippet:
5.times do i
puts i
end
medium
A. puts cannot be used inside times block
B. times method cannot be used with do...end
C. Missing block parameter pipes | | around i
D. Integer 5 cannot call times method
Solution
Step 1: Check block parameter syntax
In Ruby, block parameters must be enclosed in pipes: do |i| ... end.
Step 2: Identify the missing pipes causing syntax error
The code uses do i without pipes, which is invalid syntax.
Final Answer:
Missing block parameter pipes | | around i -> Option C
Quick Check:
Block params need |i|, not just i [OK]
Hint: Always use |param| in block headers [OK]
Common Mistakes:
Omitting pipes around block parameters
Thinking do/end can't be used with times
Misunderstanding times method usage
5. You want to create an array of squares of numbers from 0 to 4 using times. Which code correctly does this?
hard
A. squares = (0..4).times { |i| i * i }
puts squares
B. squares = 5.times { |i| i * i }
puts squares
C. squares = []
5.times do i
squares.push(i * i)
end
puts squares
D. squares = []
5.times { |i| squares << i * i }
puts squares
Solution
Step 1: Understand how to collect results in an array
Since times returns the integer, not an array, we must create an empty array and push results inside the block.
Step 2: Check each option for correct syntax and logic
squares = []
5.times { |i| squares << i * i }
puts squares initializes an empty array, uses 5.times with block parameter i, and appends i*i to the array correctly.
Final Answer:
squares = []
5.times { |i| squares << i * i }
puts squares -> Option D
Quick Check:
Use array and push inside times block [OK]
Hint: Create array first, then push inside times block [OK]
Common Mistakes:
Assuming times returns an array
Using invalid range with times
Omitting block parameter pipes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.