0
0
Rubyprogramming~15 mins

Case with ranges and patterns in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Case with ranges and patterns
📖 Scenario: You are creating a simple program to categorize ages into life stages using Ruby's case statement with ranges and pattern matching.
🎯 Goal: Build a Ruby program that uses a case statement with ranges and patterns to print the life stage for a given age.
📋 What You'll Learn
Create a variable age with a specific integer value.
Create a variable adult_age to set the minimum adult age.
Use a case statement with ranges and pattern matching to determine the life stage.
Print the life stage as output.
💡 Why This Matters
🌍 Real World
Categorizing ages into groups is common in apps like surveys, games, or health trackers.
💼 Career
Understanding <code>case</code> with ranges and patterns helps in writing clear, readable decision-making code in Ruby.
Progress0 / 4 steps
1
DATA SETUP: Create the age variable
Create a variable called age and set it to the integer 25.
Ruby
Need a hint?

Use = to assign the value 25 to the variable age.

2
CONFIGURATION: Create the adult_age variable
Create a variable called adult_age and set it to the integer 18.
Ruby
Need a hint?

Use = to assign the value 18 to the variable adult_age.

3
CORE LOGIC: Use case with ranges and pattern matching
Write a case statement using the variable age to match these ranges and patterns:
- When age is between 0 and 12 (inclusive), assign stage the string "Child".
- When age is between 13 and 17 (inclusive), assign stage the string "Teenager".
- When age is greater than or equal to adult_age, assign stage the string "Adult".
- Otherwise, assign stage the string "Unknown".
Ruby
Need a hint?

Use case age and when with ranges like 0..12. Use Float::INFINITY for open-ended ranges.

4
OUTPUT: Print the life stage
Write a puts statement to print the value of the variable stage.
Ruby
Need a hint?

Use puts stage to print the life stage.