The array contains odd numbers: 1, 3, 5, 7. The condition checks if each number is even (n % 2 == 0).
Step 2: Evaluate all? with the condition
Since none of the numbers are even, all? returns false.
Final Answer:
false -> Option D
Quick Check:
all? returns false if any element fails condition [OK]
Hint: all? needs every element true to return true [OK]
Common Mistakes:
Thinking all? returns true if some elements match
Expecting SyntaxError for correct block usage
Confusing output with nil
5.
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
A. ages.any? { |age| age < 18 }
B. ages.all? { |age| age < 18 }
C. ages.none? { |age| age < 18 }
D. ages.none? { |age| age >= 18 }
Solution
Step 1: Understand the condition to check
We want to confirm no user is under 18, meaning no age < 18.
Step 2: Choose the method that returns true if no elements match condition
none? returns true if no elements satisfy the block condition.
Step 3: Verify the block condition
The block { |age| age < 18 } checks if age is under 18.
Final Answer:
ages.none? { |age| age < 18 } -> Option C
Quick Check:
none? means no elements match condition [OK]
Hint: 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?
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.