Recall & Review
beginner
What does the
select method do in Ruby?The
select method filters elements from a collection based on a condition. It returns a new array with elements for which the condition is true.Click to reveal answer
beginner
How is
select different from reject in Ruby?select keeps elements where the condition is true, while reject keeps elements where the condition is false.Click to reveal answer
beginner
Given
numbers = [1, 2, 3, 4, 5], what does numbers.select { |n| n.even? } return?It returns
[2, 4] because it selects only the even numbers from the array.Click to reveal answer
intermediate
Can
select be used on hashes in Ruby? How?Yes,
select can be used on hashes. It returns a new hash with key-value pairs where the block condition is true.Click to reveal answer
beginner
What is the return type of
select when used on an array?It returns a new array containing the filtered elements.
Click to reveal answer
What does
select return when no elements match the condition?✗ Incorrect
select returns an empty array if no elements satisfy the condition.
Which method would you use to get elements that do NOT match a condition?
✗ Incorrect
reject returns elements where the condition is false, opposite of select.
What will
[1, 2, 3].select { |n| n > 3 } return?✗ Incorrect
No elements are greater than 3, so select returns an empty array.
Can
select change the original array?✗ Incorrect
select returns a new array and does not modify the original.
Which of these is a valid use of
select on a hash {a: 1, b: 2}?✗ Incorrect
When selecting on a hash, the block receives key and value. Checking if value is even is valid.
Explain how the
select method works in Ruby with an example.Think about choosing items that match a rule.
You got /4 concepts.
Describe the difference between
select and reject methods in Ruby.One chooses what you want, the other what you don't want.
You got /4 concepts.