0
0
Rubyprogramming~30 mins

Break, next, and redo behavior in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Break, next, and redo behavior
📖 Scenario: You are working on a simple program that processes a list of tasks. Sometimes you want to skip a task, stop processing early, or retry a task if it fails.
🎯 Goal: Build a Ruby program that demonstrates how to use break, next, and redo inside a loop to control the flow of task processing.
📋 What You'll Learn
Create an array called tasks with the exact values: ['clean', 'cook', 'shop', 'laundry', 'study']
Create a variable called skip_task and set it to the string 'shop'
Use a for loop with variable task to iterate over tasks and inside the loop:
Use next to skip the task if it matches skip_task
Use redo to retry the task 'laundry' once before moving on
Use break to stop the loop completely if the task is 'study'
Print each task when it is processed
💡 Why This Matters
🌍 Real World
Controlling loops with break, next, and redo is useful when processing lists of jobs, filtering data, or retrying failed operations in real programs.
💼 Career
Understanding these flow controls helps in debugging, writing efficient loops, and managing complex workflows in software development.
Progress0 / 4 steps
1
Create the tasks array
Create an array called tasks with these exact string values: 'clean', 'cook', 'shop', 'laundry', and 'study'
Ruby
Need a hint?

Use square brackets [] to create an array and separate items with commas.

2
Add the skip_task variable
Create a variable called skip_task and set it to the string 'shop'
Ruby
Need a hint?

Assign the string 'shop' to the variable skip_task.

3
Loop through tasks and skip the skip_task
Use a for loop with variable task to iterate over tasks. Inside the loop, use next to skip the task if it matches skip_task. Print each task that is processed.
Ruby
Need a hint?

Use next to skip the current loop iteration when task equals skip_task.

4
Add redo and break behavior
Modify the loop to retry the task 'laundry' once using redo. Also, use break to stop the loop completely if the task is 'study'. Print each task when it is processed.
Ruby
Need a hint?

Use a hash redo_counts to track retries. Use redo to repeat the 'laundry' task once. Use break to stop when task is 'study'.