📖 Scenario: You are creating a simple program to check if a number is even or odd. This is like deciding if a number is like a pair of shoes (even) or a single shoe (odd).
🎯 Goal: Build a Ruby program that uses the ternary operator to decide if a number is even or odd and prints the result.
📋 What You'll Learn
Create a variable called number with the value 7.
Create a variable called result that uses the ternary operator to check if number is even or odd.
Print the result variable.
💡 Why This Matters
🌍 Real World
Ternary operators help write short and clear decisions in code, like quickly choosing between two options.
💼 Career
Many programming jobs require writing clean and concise code. Using ternary operators is a common skill for making decisions in your programs.
Progress0 / 4 steps
1
DATA SETUP: Create the number variable
Create a variable called number and set it to 7.
Ruby
Hint
Use = to assign the value 7 to the variable number.
2
CONFIGURATION: Prepare the ternary operator
Create a variable called result that uses the ternary operator to check if number is even. If it is even, set result to "Even", otherwise set it to "Odd".
Ruby
Hint
Use condition ? value_if_true : value_if_false format. The condition here is number.even?.
3
CORE LOGIC: Use the ternary operator correctly
Make sure the ternary operator uses number.even? as the condition, and assigns "Even" or "Odd" to result accordingly.
Ruby
Hint
Check that the ternary operator is written exactly as number.even? ? "Even" : "Odd".
4
OUTPUT: Print the result
Write a puts statement to print the value of result.
Ruby
Hint
Use puts result to show the output.
Practice
(1/5)
1. What does the ternary operator condition ? value_if_true : value_if_false do in Ruby?
easy
A. It chooses one of two values based on the condition's truth.
B. It creates a loop that runs twice.
C. It defines a new method with two parameters.
D. It comments out the code after the colon.
Solution
Step 1: Understand the ternary operator format
The ternary operator uses a condition followed by a question mark, then two values separated by a colon.
Step 2: Identify its purpose
It returns the first value if the condition is true, otherwise the second value.
Final Answer:
It chooses one of two values based on the condition's truth. -> Option A
Quick Check:
condition ? true_value : false_value = chooses value [OK]
Hint: Think: condition true? pick first value, else second [OK]
Common Mistakes:
Confusing ternary with loops
Thinking it defines methods
Misunderstanding the colon as comment
2. Which of the following is the correct syntax for a ternary operator in Ruby?
easy
A. condition ? value_if_true : value_if_false
B. condition : value_if_true ? value_if_false
C. condition ? value_if_true ; value_if_false
D. condition ? value_if_true, value_if_false
Solution
Step 1: Recall ternary operator syntax
The correct syntax places a question mark after the condition, then the true value, a colon, then the false value.
Step 2: Compare options
Only condition ? value_if_true : value_if_false matches the correct format: condition ? true_value : false_value.
Final Answer:
condition ? value_if_true : value_if_false -> Option A
Quick Check:
Syntax = condition ? true : false [OK]
Hint: Remember: question mark before true, colon before false [OK]
Common Mistakes:
Swapping question mark and colon
Using semicolon instead of colon
Separating values with comma
3. What is the output of this Ruby code?
age = 20
puts age >= 18 ? "Adult" : "Minor"
medium
A. Minor
B. 20
C. SyntaxError
D. Adult
Solution
Step 1: Evaluate the condition
The condition age >= 18 is true because age is 20.
Step 2: Apply ternary operator
Since the condition is true, the expression returns "Adult".
Final Answer:
Adult -> Option D
Quick Check:
20 >= 18 ? "Adult" : "Minor" = "Adult" [OK]
Hint: Check condition true? pick first value, else second [OK]
Common Mistakes:
Choosing the false value by mistake
Printing the variable instead of result
Expecting syntax error
4. Identify the error in this Ruby code using a ternary operator:
score = 85
result = score > 60 ? "Pass" "Fail"
puts result
medium
A. Missing question mark after condition
B. Missing colon between true and false values
C. Ternary operator cannot be used with strings
D. Wrong variable name 'score'
Solution
Step 1: Check ternary operator syntax
The ternary operator requires a colon between the true and false values.
Step 2: Identify missing colon
The code has "Pass" "Fail" without a colon, causing a syntax error.
Final Answer:
Missing colon between true and false values -> Option B