0
0
Rubyprogramming~30 mins

Case/when statement in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Using case/when statement in Ruby
📖 Scenario: You are building a simple program that gives a message based on the day of the week. This is like a small helper that tells you what kind of day it is.
🎯 Goal: Create a Ruby program that uses a case/when statement to print a message depending on the day stored in a variable.
📋 What You'll Learn
Create a variable called day with the exact value "Tuesday".
Create a variable called weekend_days that holds an array with "Saturday" and "Sunday".
Use a case statement on the variable day with when clauses for weekdays and weekend days.
Print the exact message "It's a weekday." if the day is Monday to Friday.
Print the exact message "It's the weekend!" if the day is Saturday or Sunday.
Print the exact message "Unknown day." if the day is not recognized.
💡 Why This Matters
🌍 Real World
Using <code>case/when</code> statements helps programs make decisions based on different conditions, like choosing what to do depending on user input or data values.
💼 Career
Understanding <code>case/when</code> statements is important for writing clear and efficient code in Ruby, which is used in web development, automation, and many software projects.
Progress0 / 4 steps
1
Create the day variable
Create a variable called day and set it to the string "Tuesday".
Ruby
Need a hint?

Use = to assign the string "Tuesday" to the variable day.

2
Create the weekend_days array
Create a variable called weekend_days and set it to an array containing the strings "Saturday" and "Sunday".
Ruby
Need a hint?

Use square brackets [] to create an array with the two weekend day strings.

3
Write the case statement for day
Write a case statement using the variable day. Use when clauses for weekdays ("Monday" to "Friday") to print "It's a weekday.". Use a when clause for weekend_days to print "It's the weekend!". Use else to print "Unknown day.".
Ruby
Need a hint?

Use when with multiple values separated by commas for weekdays. Use when *weekend_days to match weekend days. Use else for any other day.

4
Print the result
Run the program to print the message for the day stored in day. The output should be exactly It's a weekday..
Ruby
Need a hint?

Make sure you run the program and check the output matches exactly.