Bird
0
0

You want to print "Access denied" only when a user is NOT an admin. Which Ruby code using unless is correct?

hard📝 Application Q15 of 15
Ruby - Control Flow
You want to print "Access denied" only when a user is NOT an admin. Which Ruby code using unless is correct?
Aunless user.admin? puts 'Access denied' end
Bunless user.admin? puts 'Access granted' end
Cif !user.admin? puts 'Access denied' end
Dif user.admin? puts 'Access denied' end
Step-by-Step Solution
Solution:
  1. Step 1: Understand the condition needed

    We want to print 'Access denied' only if user is NOT admin.
  2. Step 2: Match condition with unless

    unless user.admin? runs block if user.admin? is false, exactly when user is not admin.
  3. Step 3: Check other options

    unless user.admin? puts 'Access granted' end prints wrong message, C uses if with negation (correct but not using unless), D prints denied when user is admin (wrong).
  4. Final Answer:

    unless user.admin? puts 'Access denied' end -> Option A
  5. Quick Check:

    Use unless for negated condition [OK]
Quick Trick: Use unless for simple negated conditions [OK]
Common Mistakes:
MISTAKES
  • Mixing messages with wrong conditions
  • Using if with negation instead of unless
  • Printing denied when user is admin

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes