📖 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
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
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
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
Hint
Use unless at the end of the print statement to check the opposite condition.
Practice
(1/5)
1. What does the following Ruby code do? puts "Hello" if true
easy
A. Prints "Hello" because the condition is true
B. Prints nothing because the condition is false
C. Raises a syntax error
D. Prints "Hello" only if false
Solution
Step 1: Understand the inline if condition
The code uses if true, so the condition is always true.
Step 2: Determine the action based on the condition
Since the condition is true, puts "Hello" runs and prints "Hello".
Final Answer:
Prints "Hello" because the condition is true -> Option A
Quick Check:
Inline if with true condition prints output [OK]
Hint: If condition is true, inline if runs the code [OK]
Common Mistakes:
Thinking it prints only if condition is false
Confusing inline if with inline unless
Expecting syntax error for inline if
2. Which of the following is the correct syntax for using inline unless in Ruby?
easy
A. puts "No" unless false
B. puts "No" unless
C. puts "No"unless (false)
D. puts "No" unless: false
Solution
Step 1: Recall inline unless syntax
Inline unless requires a condition after the keyword, like unless false.
Step 2: Check each option
puts "No" unless false is valid syntax. puts "No"unless (false) puts "No"unless (false) is invalid due to missing space after the string literal. Options B and D are incomplete or invalid.
Final Answer:
puts "No" unless false -> Option A
Quick Check:
Inline unless needs a condition after it [OK]
Hint: Inline unless needs a condition right after it [OK]
count += 1 if false does not run because condition is false, so count stays 0.
Step 2: Evaluate second increment with inline unless
count += 1 unless false runs because unless false means if true, so count becomes 1.
Step 3: Print the final count
puts count prints 1.
Final Answer:
1 -> Option C
Quick Check:
Inline if false skips, inline unless false runs [OK]
Hint: Inline unless runs when condition is false [OK]
Common Mistakes:
Thinking inline if false runs the code
Confusing unless with if
Expecting count to be 2
4. Identify the error in this Ruby code:
puts "Done" unless
medium
A. Incorrect use of puts
B. Missing condition after unless
C. Should use if instead of unless
D. No error, code runs fine
Solution
Step 1: Check the inline unless syntax
Inline unless requires a condition after it to decide when to run the code.
Step 2: Analyze the given code
The code ends with unless but no condition follows, causing a syntax error.
Final Answer:
Missing condition after unless -> Option B
Quick Check:
Inline unless must have a condition [OK]
Hint: Always put a condition after unless [OK]
Common Mistakes:
Leaving out the condition after unless
Thinking puts usage is wrong
Confusing syntax error with runtime error
5. Given the array nums = [0, 1, 2, 3], which code snippet correctly prints only the non-zero numbers using inline unless?
hard
A. nums.each { |n| puts n if n != 0 }
B. nums.each { |n| puts n if n == 0 }
C. nums.each { |n| puts n unless n != 0 }
D. nums.each { |n| puts n unless n == 0 }
Solution
Step 1: Understand the goal
We want to print numbers except zero, so skip zero and print others.
Step 2: Analyze each option
nums.each { |n| puts n unless n == 0 } uses puts n unless n == 0, which prints n only if n is not zero. nums.each { |n| puts n if n == 0 } prints only zero, which is wrong. nums.each { |n| puts n unless n != 0 } prints only zero because unless n != 0 means if n == 0. nums.each { |n| puts n if n != 0 } prints non-zero numbers but uses inline if, not unless.
Final Answer:
nums.each { |n| puts n unless n == 0 } -> Option D
Quick Check:
Inline unless skips zero, prints others [OK]
Hint: Use unless with condition to skip unwanted values [OK]
Common Mistakes:
Confusing unless with if
Printing zero instead of skipping
Using wrong condition logic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.