Challenge - 5 Problems
Ruby Select/Filter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Ruby code using select?
Consider the following Ruby code that filters numbers greater than 3 from an array. What will be printed?
Ruby
numbers = [1, 2, 3, 4, 5] result = numbers.select { |n| n > 3 } puts result.inspect
Attempts:
2 left
💡 Hint
Remember, select keeps elements where the block returns true.
✗ Incorrect
The select method returns a new array containing all elements for which the block returns true. Here, only 4 and 5 are greater than 3.
❓ Predict Output
intermediate2:00remaining
What does this filter code output?
Given this Ruby code using filter (alias for select), what is the output?
Ruby
words = ['apple', 'banana', 'cherry', 'date'] filtered = words.filter { |w| w.length == 5 } puts filtered.inspect
Attempts:
2 left
💡 Hint
Check the length of each word carefully.
✗ Incorrect
Only 'apple' has exactly 5 letters. 'cherry' has 6 letters, so it is not included.
❓ Predict Output
advanced2:30remaining
What is the output when filtering hashes with select?
What will this Ruby code print?
Ruby
people = [{name: 'Alice', age: 30}, {name: 'Bob', age: 20}, {name: 'Carol', age: 25}]
adults = people.select { |p| p[:age] >= 25 }
puts adults.map { |p| p[:name] }.inspectAttempts:
2 left
💡 Hint
Select people whose age is 25 or more.
✗ Incorrect
Alice is 30 and Carol is 25, so both are included. Bob is 20, so excluded.
❓ Predict Output
advanced2:00remaining
What error does this code raise?
What error will this Ruby code raise when trying to filter with a wrong block?
Ruby
numbers = [1, 2, 3] result = numbers.select { |n| n > '2' } puts result.inspect
Attempts:
2 left
💡 Hint
Compare integer with string causes a problem.
✗ Incorrect
Comparing integer n with string '2' using > causes a TypeError in Ruby.
🧠 Conceptual
expert2:30remaining
How many elements are in the result of this chained filter?
Given this Ruby code, how many elements are in the final array?
Ruby
arr = [10, 15, 20, 25, 30] result = arr.select { |x| x > 10 }.select { |x| x % 10 == 0 }
Attempts:
2 left
💡 Hint
First filter keeps numbers greater than 10, second keeps multiples of 10.
✗ Incorrect
Numbers greater than 10 are [15, 20, 25, 30]. Among these, multiples of 10 are 20 and 30, so 2 elements.