Challenge - 5 Problems
Ruby Inline If Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of inline if modifier
What will be the output of this Ruby code snippet?
Ruby
message = "Hello" message += " World" if message.length < 10 puts message
Attempts:
2 left
💡 Hint
Check the length of the string before the condition.
✗ Incorrect
The condition checks if the length of 'message' is less than 10, which is true (5). So ' World' is added.
❓ Predict Output
intermediate2:00remaining
Using inline unless modifier
What will this Ruby code print?
Ruby
count = 5 count -= 1 unless count > 5 puts count
Attempts:
2 left
💡 Hint
Remember 'unless' runs the code only if the condition is false.
✗ Incorrect
Since count (5) is not greater than 5, the condition is false, so count is decreased by 1.
🔧 Debug
advanced2:00remaining
Identify the error in inline if usage
Which option shows the code that will raise a syntax error?
Attempts:
2 left
💡 Hint
Inline if cannot be combined with else in this way.
✗ Incorrect
Option D tries to use 'else' with inline if, which is invalid syntax in Ruby.
🧠 Conceptual
advanced2:00remaining
Effect of inline unless modifier
What is the value of variable 'status' after running this code?
Ruby
status = "active" status = "inactive" unless status == "inactive"
Attempts:
2 left
💡 Hint
The 'unless' runs the assignment only if the condition is false.
✗ Incorrect
Since status is "active", which is not "inactive", the assignment runs and status becomes "inactive".
❓ Predict Output
expert2:00remaining
Output with inline if and method call
What will this Ruby code output?
Ruby
def check(num) puts "Even" if num.even? puts "Odd" unless num.even? end check(3)
Attempts:
2 left
💡 Hint
Check what num.even? returns for 3.
✗ Incorrect
3 is odd, so num.even? is false. The first puts is skipped, the second runs printing 'Odd'.