Bird
0
0

You want to filter out falsy values from this array in Ruby:

hard📝 Application Q15 of 15
Ruby - Operators and Expressions
You want to filter out falsy values from this array in Ruby:
arr = [false, nil, 0, "", [], true]
Which code correctly returns only truthy values?
Aarr.select { |v| v }
Barr.reject { |v| v }
Carr.select { |v| v != false }
Darr.select { |v| v == true }
Step-by-Step Solution
Solution:
  1. Step 1: Understand filtering truthy values

    Using select { |v| v } keeps only truthy values because v in condition is true for truthy values.
  2. Step 2: Analyze other options

    arr.select { |v| v != false } keeps nil (nil != false); arr.reject { |v| v } rejects truthy values; arr.select { |v| v == true } keeps only true, missing other truthy values.
  3. Final Answer:

    arr.select { |v| v } -> Option A
  4. Quick Check:

    Use select with block returning value for truthy filter [OK]
Quick Trick: Use select { |v| v } to keep truthy values [OK]
Common Mistakes:
  • Using reject instead of select
  • Checking explicitly for false instead of truthiness
  • Selecting only true, missing other truthy values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes