Bird
0
0

How can you use Ruby's any?, all?, and none? methods together to check if an array of strings contains at least one empty string, all strings have length less than 10, and none are nil?

hard📝 Application Q9 of 15
Ruby - Enumerable and Collection Processing
How can you use Ruby's any?, all?, and none? methods together to check if an array of strings contains at least one empty string, all strings have length less than 10, and none are nil?
Aarr.none?(&:empty?) || arr.all? { |s| s.length < 10 } || arr.any?(&:nil?)
Barr.any?(&:empty?) && arr.all? { |s| s.length < 10 } && arr.none?(&:nil?)
Carr.all?(&:empty?) && arr.any? { |s| s.length < 10 } && arr.none?(&:nil?)
Darr.any?(&:empty?) || arr.none? { |s| s.length < 10 } && arr.all?(&:nil?)
Step-by-Step Solution
Solution:
  1. Step 1: Break down each condition

    Check if any string is empty, all strings have length less than 10, and none are nil.
  2. Step 2: Use predicates correctly

    any?(&:empty?) checks for empty strings, all? { |s| s.length < 10 } checks length, and none?(&:nil?) ensures no nil values.
  3. Final Answer:

    arr.any?(&:empty?) && arr.all? { |s| s.length < 10 } && arr.none?(&:nil?) -> Option B
  4. Quick Check:

    Combine predicates with && for all conditions [OK]
Quick Trick: Use && to combine any?, all?, none? for multiple checks [OK]
Common Mistakes:
  • Using || instead of && to combine conditions
  • Mixing up any? and none? usage
  • Incorrect block syntax for predicates

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes