Bird
0
0

Find the mistake in this Ruby method using a guard clause:

medium📝 Debug Q6 of 15
Ruby - Control Flow
Find the mistake in this Ruby method using a guard clause:
def validate(input)
  return "Empty input" if input = ""
  "Input is valid"
end
AMissing parentheses around the condition in the guard clause
BUsing assignment '=' instead of comparison '==' in the guard clause
CThe guard clause should return nil instead of a string
DThe method should use 'unless' instead of 'if'
Step-by-Step Solution
Solution:
  1. Step 1: Review guard clause syntax

    The guard clause condition should use comparison operators to check values.
  2. Step 2: Identify error

    The code uses = which is assignment, not comparison. It should be == to compare input with an empty string.
  3. Final Answer:

    Using assignment '=' instead of comparison '==' in the guard clause -> Option B
  4. Quick Check:

    Use '==' for comparisons, not '=' [OK]
Quick Trick: Use '==' for comparisons in guard clauses [OK]
Common Mistakes:
  • Confusing assignment '=' with equality '=='
  • Assuming parentheses are mandatory in guard clauses
  • Thinking guard clauses must return nil

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes