Bird
0
0

You have a list of user ages: ages = [12, 17, 20, 25, 30]. You want to check if none of the users are under 18 years old. Which Ruby code correctly performs this check?

hard📝 Application Q15 of 15
Ruby - Enumerable and Collection Processing

You have a list of user ages: ages = [12, 17, 20, 25, 30]. You want to check if none of the users are under 18 years old. Which Ruby code correctly performs this check?

Aages.any? { |age| age < 18 }
Bages.all? { |age| age < 18 }
Cages.none? { |age| age < 18 }
Dages.none? { |age| age >= 18 }
Step-by-Step Solution
Solution:
  1. Step 1: Understand the condition to check

    We want to confirm no user is under 18, meaning no age < 18.
  2. Step 2: Choose the method that returns true if no elements match condition

    none? returns true if no elements satisfy the block condition.
  3. Step 3: Verify the block condition

    The block { |age| age < 18 } checks if age is under 18.
  4. Final Answer:

    ages.none? { |age| age < 18 } -> Option C
  5. Quick Check:

    none? means no elements match condition [OK]
Quick Trick: Use none? to confirm no elements meet condition [OK]
Common Mistakes:
  • Using any? which checks if some match
  • Using all? which requires all to match
  • Reversing the condition inside none?

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes