Bird
0
0

Given an array of numbers, how would you use Ruby predicates to check if none of the numbers are zero and all are positive?

hard📝 Application Q8 of 15
Ruby - Enumerable and Collection Processing
Given an array of numbers, how would you use Ruby predicates to check if none of the numbers are zero and all are positive?
Aarr.any? { |n| n == 0 } && arr.all? { |n| n > 0 }
Barr.none? { |n| n == 0 } && arr.all? { |n| n > 0 }
Carr.none? { |n| n > 0 } || arr.all? { |n| n == 0 }
Darr.all? { |n| n == 0 } && arr.none? { |n| n > 0 }
Step-by-Step Solution
Solution:
  1. Step 1: Understand the conditions

    We want to confirm no zeros and all positive numbers.
  2. Step 2: Use none? to check no zeros and all? to check positivity

    none? { |n| n == 0 } ensures no zeros, and all? { |n| n > 0 } ensures all are positive.
  3. Final Answer:

    arr.none? { |n| n == 0 } && arr.all? { |n| n > 0 } -> Option B
  4. Quick Check:

    Combine none? and all? with && for multiple conditions [OK]
Quick Trick: Combine none? and all? with && for multiple checks [OK]
Common Mistakes:
  • Using any? instead of none? for zero check
  • Mixing conditions with || instead of &&
  • Reversing conditions in predicates

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes