Bird
0
0

Which of the following is the correct syntax to check if a variable x is falsy in Ruby?

easy📝 Syntax Q3 of 15
Ruby - Operators and Expressions
Which of the following is the correct syntax to check if a variable x is falsy in Ruby?
Aif !x
Bif x == false
Cif x.nil?
Dif x != true
Step-by-Step Solution
Solution:
  1. Step 1: Understand Ruby falsy check

    In Ruby, !x returns true if x is falsy (false or nil).
  2. Step 2: Analyze options

    if x == false only checks if value is exactly false, missing nil. if x.nil? only checks for nil, missing false. if x != true returns true for truthy values like 0, incorrectly treating them as falsy. if !x is idiomatic and correct.
  3. Final Answer:

    Using if !x is the correct and idiomatic way -> Option A
  4. Quick Check:

    Falsy check = if !x [OK]
Quick Trick: Use if !x to check falsy values simply [OK]
Common Mistakes:
MISTAKES
  • Using == false misses nil
  • Using x.nil? misses false
  • Using != true incorrectly treats truthy as falsy

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes