Bird
0
0

Which of these is a correct guard clause to return false if the input is nil?

easy📝 Conceptual Q2 of 15
Ruby - Control Flow
Which of these is a correct guard clause to return false if the input is nil?
Areturn false if input.nil?
Bif input.nil? then false
Creturn false unless input.nil?
Dif input == nil return false
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct guard clause syntax

    In Ruby, guard clauses use 'return' followed by condition and action in one line.
  2. Step 2: Check each option

    return false if input.nil? uses 'return false if input.nil?' which is correct syntax and logic.
  3. Final Answer:

    return false if input.nil? -> Option A
  4. Quick Check:

    Guard clause syntax = 'return ... if condition' [OK]
Quick Trick: Use 'return value if condition' for guard clauses [OK]
Common Mistakes:
MISTAKES
  • Omitting 'return' keyword
  • Using 'unless' incorrectly
  • Writing multi-line if without return

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes