0
0
Rubyprogramming~10 mins

Ternary operator in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the Ternary Operator in Ruby
📖 Scenario: You are creating a simple program to check if a person is old enough to vote. Voting age is 18 years or older.
🎯 Goal: Build a Ruby program that uses the ternary operator to decide if a person can vote based on their age.
📋 What You'll Learn
Create a variable called age with a specific integer value.
Create a variable called voting_age set to 18.
Use the ternary operator to create a variable can_vote that holds the string "Yes" if age is greater than or equal to voting_age, otherwise "No".
Print the value of can_vote.
💡 Why This Matters
🌍 Real World
Checking conditions quickly is common in programs, like deciding if someone can access a service based on age.
💼 Career
Understanding the ternary operator helps write concise and readable code, a skill valued in many programming jobs.
Progress0 / 4 steps
1
Set the person's age
Create a variable called age and set it to the integer 20.
Ruby
Need a hint?

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

2
Set the voting age limit
Create a variable called voting_age and set it to the integer 18.
Ruby
Need a hint?

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

3
Use the ternary operator to check voting eligibility
Use the ternary operator to create a variable called can_vote that is "Yes" if age is greater than or equal to voting_age, otherwise "No".
Ruby
Need a hint?

The ternary operator format is condition ? value_if_true : value_if_false.

4
Print the voting eligibility
Print the value of the variable can_vote.
Ruby
Need a hint?

Use puts to print the value of can_vote.