Bird
0
0

Identify the bug in this Ruby code:

medium📝 Debug Q7 of 15
Ruby - Operators and Expressions
Identify the bug in this Ruby code:
value = false
if value.nil?
  puts "Falsy"
else
  puts "Truthy"
end
AUse 'if value == false' to check falsy
BNo bug, code works as expected
CShould check 'if !value' instead of 'value.nil?'
DReplace false with nil to fix bug
Step-by-Step Solution
Solution:
  1. Step 1: Understand what 'value.nil?' checks

    The method value.nil? returns true only if value is nil, but here value is false.
  2. Step 2: Correct falsy check

    Since false is also falsy, use if !value to check both false and nil.
  3. Final Answer:

    Should check 'if !value' instead of 'value.nil?' -> Option C
  4. Quick Check:

    Falsy check needs if !value [OK]
Quick Trick: value.nil? misses false, use if !value [OK]
Common Mistakes:
MISTAKES
  • Using value.nil? misses false
  • Checking only == false misses nil
  • Replacing false with nil incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes