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 5Create a variable called
message and set it to the string "Cupcake baked!"Use the
times method on cupcakes to repeat a block 5 timesInside the block, print the
message followed by the cupcake number starting from 1Print 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