Bird
0
0

Identify the error in this Ruby method using a guard clause:

medium📝 Debug Q14 of 15
Ruby - Control Flow
Identify the error in this Ruby method using a guard clause:
def greet(name)
  return "No name provided" if name = nil
  "Hello, #{name}!"
end
AIncorrect string interpolation syntax
BUsing assignment (=) instead of comparison (==) in guard clause
CMissing else statement after guard clause
DGuard clause should be after the greeting line
Step-by-Step Solution
Solution:
  1. Step 1: Check guard clause condition

    The guard clause uses name = nil, which assigns nil to name instead of comparing.
  2. Step 2: Identify correct comparison operator

    To check if name is nil, it should use name == nil or better name.nil?.
  3. Final Answer:

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

    Use '==' or '.nil?' for comparisons, not '=' [OK]
Quick Trick: Use '==' to compare, '=' assigns value [OK]
Common Mistakes:
  • Confusing '=' with '==' in conditions
  • Thinking else is required after guard clause
  • Misusing string interpolation syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes