0
0
Rubyprogramming~10 mins

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

Choose your learning style9 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.