0
0
Rubyprogramming~15 mins

Times method for counted repetition in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Make sure your program prints exactly 5 lines, each with the message and the correct cupcake number.