Bird
0
0

Which of the following is the correct way to check if a variable x is truthy in Ruby?

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

    In Ruby, if x automatically checks if x is truthy (not false or nil).
  2. Step 2: Analyze options

    if x == true only matches true, missing other truthy values; if x != nil treats false as truthy (false != nil); if x == nil checks the opposite of truthy.
  3. Final Answer:

    if x -> Option B
  4. Quick Check:

    Use if x for truthy check [OK]
Quick Trick: Use 'if x' to check truthiness simply [OK]
Common Mistakes:
MISTAKES
  • Using 'if x == true' which misses other truthy values
  • Using 'if x != nil' which treats false as truthy
  • Checking 'if x == nil' to test truthiness

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes