0
0
Rubyprogramming~20 mins

Select/filter for filtering in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Select/Filter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
A[1, 2, 3]
B[4, 5]
C[]
D[3, 4, 5]
Attempts:
2 left
💡 Hint
Remember, select keeps elements where the block returns true.
Predict Output
intermediate
2: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
A["apple"]
B["apple", "cherry"]
C["banana", "date"]
D["apple", "banana", "cherry", "date"]
Attempts:
2 left
💡 Hint
Check the length of each word carefully.
Predict Output
advanced
2: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] }.inspect
A["Alice", "Carol"]
B["Bob", "Carol"]
C["Alice", "Bob"]
D["Carol"]
Attempts:
2 left
💡 Hint
Select people whose age is 25 or more.
Predict Output
advanced
2: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
AArgumentError
BNo error, outputs [3]
CTypeError
DNoMethodError
Attempts:
2 left
💡 Hint
Compare integer with string causes a problem.
🧠 Conceptual
expert
2: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 }
A1
B3
C4
D2
Attempts:
2 left
💡 Hint
First filter keeps numbers greater than 10, second keeps multiples of 10.