Bird
Raised Fist0
Rubyprogramming~10 mins

Any?, all?, none? predicates in Ruby - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if any number in the array is even.

Ruby
numbers = [1, 3, 5, 6, 7]
result = numbers.[1]? { |n| n.even? }
puts result
Drag options to blanks, or click blank then click option'
Aselect
Ball
Cnone
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using all? instead of any? which checks all elements.
Using none? which checks if no elements match.
2fill in blank
medium

Complete the code to check if all words in the array start with a capital letter.

Ruby
words = ["Apple", "Banana", "Cherry"]
result = words.[1]? { |word| word[0] == word[0].upcase }
puts result
Drag options to blanks, or click blank then click option'
Anone
Ball
Cany
Dfind
Attempts:
3 left
💡 Hint
Common Mistakes
Using any? which returns true if just one element matches.
Using none? which returns true if no elements match.
3fill in blank
hard

Fix the error in the code to check if no numbers in the array are negative.

Ruby
numbers = [0, 1, 2, 3]
result = numbers.[1]? { |n| n < 0 }
puts result
Drag options to blanks, or click blank then click option'
Aany
Ball
Cnone
Dfind
Attempts:
3 left
💡 Hint
Common Mistakes
Using any? which returns true if any element matches.
Using all? which requires all elements to match.
4fill in blank
hard

Fill both blanks to create a hash of words and their lengths, including only words longer than 3 letters.

Ruby
words = ["cat", "elephant", "dog", "lion"]
lengths = { [1] => [2] for word in words if word.length > 3 }
puts lengths
Drag options to blanks, or click blank then click option'
Aword
Bword.length
Cword.size
Dword.upcase
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.upcase as a key or value incorrectly.
Using word.size instead of word.length (both work but only one is correct here).
5fill in blank
hard

Fill all three blanks to create a hash of uppercase words and their lengths, including only words longer than 3 letters.

Ruby
words = ["cat", "elephant", "dog", "lion"]
lengths = { [1] => [2] for word in words if [2] [3] 3 }
puts lengths
Drag options to blanks, or click blank then click option'
Aword.upcase
Bword.length
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > in the condition.
Using word instead of word.upcase as keys.

Practice

(1/5)
1.

Which Ruby method returns true if at least one element in a collection meets a condition?

easy
A. all?
B. any?
C. none?
D. each?

Solution

  1. Step 1: Understand the meaning of any?

    any? returns true if at least one element satisfies the condition.
  2. Step 2: Compare with other methods

    all? requires all elements to satisfy, none? requires none to satisfy, and each? is not a Ruby predicate method.
  3. Final Answer:

    any? -> Option B
  4. Quick Check:

    any? = at least one true [OK]
Hint: any? means at least one true element [OK]
Common Mistakes:
  • Confusing any? with all?
  • Thinking none? means at least one
  • Using each? which is not a predicate
2.

Which of the following is the correct syntax to check if all elements in an array are even numbers?

numbers = [2, 4, 6, 8]
# Choose the correct line below
easy
A. numbers.all? { |n| n.even? }
B. numbers.none? { |n| n.even? }
C. numbers.any? { |n| n.even? }
D. numbers.each? { |n| n.even? }

Solution

  1. Step 1: Identify the method to check all elements

    To verify every element meets a condition, use all?.
  2. Step 2: Confirm the block checks evenness

    The block { |n| n.even? } correctly tests if each number is even.
  3. Final Answer:

    numbers.all? { |n| n.even? } -> Option A
  4. Quick Check:

    all? checks every element [OK]
Hint: Use all? with a block to check every item [OK]
Common Mistakes:
  • Using any? instead of all?
  • Using none? which checks for no matches
  • Using each? which is not a predicate
3.

What is the output of the following Ruby code?

words = ["apple", "banana", "cherry"]
result = words.none? { |w| w.length > 6 }
puts result
medium
A. true
B. false
C. nil
D. SyntaxError

Solution

  1. Step 1: Evaluate the block condition for each word

    Check if any word has length greater than 6: "banana" (6 letters) is not >6, "cherry" (6 letters) is not >6, "apple" (5 letters) is not >6.
  2. Step 2: Determine if none? returns true or false

    Since no word has length > 6, none? returns true. But "banana" and "cherry" have length exactly 6, not greater than 6.
  3. Step 3: Re-examine condition carefully

    None have length > 6, so none? returns true.
  4. Final Answer:

    true -> Option A
  5. Quick Check:

    none? means no elements match condition [OK]
Hint: none? returns true if no elements match condition [OK]
Common Mistakes:
  • Confusing none? with any?
  • Miscounting string lengths
  • Assuming none? returns false if some equal condition
4.

Identify the error in this Ruby code snippet:

numbers = [1, 3, 5, 7]
puts numbers.all? { |n| n % 2 == 0 }

What is the output and why?

medium
A. true because all numbers are even
B. nil because no return value
C. SyntaxError due to block misuse
D. false because none of the numbers are even

Solution

  1. Step 1: Analyze the array and condition

    The array contains odd numbers: 1, 3, 5, 7. The condition checks if each number is even (n % 2 == 0).
  2. Step 2: Evaluate all? with the condition

    Since none of the numbers are even, all? returns false.
  3. Final Answer:

    false -> Option D
  4. 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

  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]
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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard