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