0
0
Rubyprogramming~15 mins

Inline if and unless (modifier form) in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Inline if and unless (modifier form) in Ruby
📖 Scenario: You are creating a simple program to check if a person is old enough to vote. You want to use Ruby's inline if and unless modifier forms to make your code short and clear.
🎯 Goal: Build a Ruby program that uses inline if and unless modifiers to print messages based on a person's age.
📋 What You'll Learn
Create a variable age with the exact value 20
Create a variable voting_age with the exact value 18
Use inline if modifier to print 'You can vote!' if age is greater than or equal to voting_age
Use inline unless modifier to print 'You cannot vote yet.' if age is less than voting_age
Print the messages exactly as specified
💡 Why This Matters
🌍 Real World
Inline if and unless modifiers help write clean and readable code for simple conditions, like checking user input or status.
💼 Career
Many Ruby developers use these modifiers to keep code concise and easy to understand, especially in web development with frameworks like Ruby on Rails.
Progress0 / 4 steps
1
Create the age variable
Create a variable called age and set it to 20.
Ruby
Need a hint?

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

2
Create the voting_age variable
Create a variable called voting_age and set it to 18.
Ruby
Need a hint?

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

3
Use inline if modifier to print voting message
Use puts "You can vote!" if age >= voting_age to print the message when age is greater than or equal to voting_age.
Ruby
Need a hint?

Write the print statement with if at the end to make it inline.

4
Use inline unless modifier to print non-voting message
Use puts "You cannot vote yet." unless age >= voting_age to print the message when age is less than voting_age. Then run the program to see the output.
Ruby
Need a hint?

Use unless at the end of the print statement to check the opposite condition.