Challenge - 5 Problems
Ruby Predicate Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of any? with a block
What is the output of this Ruby code?
Ruby
numbers = [1, 3, 5, 7] result = numbers.any? { |n| n.even? } puts result
Attempts:
2 left
💡 Hint
Think about whether any number in the array is even.
✗ Incorrect
The any? method returns true if at least one element satisfies the block condition. Here, none are even, so it returns false.
❓ Predict Output
intermediate2:00remaining
Result of all? without a block
What does this Ruby code print?
Ruby
values = [true, 1, 'hello'] puts values.all?
Attempts:
2 left
💡 Hint
Without a block, all? checks truthiness of each element.
✗ Incorrect
The all? method without a block returns true if all elements are truthy. Here, all elements are truthy, so it prints true.
❓ Predict Output
advanced2:00remaining
none? with a block returning false
What is the output of this Ruby code?
Ruby
words = ['apple', 'banana', 'cherry'] result = words.none? { |w| w.length > 5 } puts result
Attempts:
2 left
💡 Hint
Check if any word has length greater than 5.
✗ Incorrect
The none? method returns true if no elements satisfy the block. Here, 'banana' and 'cherry' have length > 5, so none? returns false.
🧠 Conceptual
advanced2:00remaining
Understanding all? with mixed truthy and falsy values
Given the array
[nil, true, false], what does all? return when called without a block?Attempts:
2 left
💡 Hint
Remember that
all? checks truthiness of each element.✗ Incorrect
Since nil and false are falsy in Ruby, all? returns false when called without a block.
❓ Predict Output
expert3:00remaining
Complex predicate with any?, all?, none?
What is the output of this Ruby code?
Ruby
data = [0, nil, false, ''] result = [ data.any?, data.all?, data.none? ] puts result.inspect
Attempts:
2 left
💡 Hint
Recall that only
false and nil are falsy; zero and empty string are truthy.✗ Incorrect
any? returns true because 0 and '' are truthy.all? returns false because nil and false are falsy.none? returns false because there are truthy elements (0 and '').