Recall & Review
beginner
What does the
reject method do in Ruby?The
reject method returns a new array excluding elements for which the given block returns true. It filters out unwanted elements.Click to reveal answer
beginner
How is
reject related to select in Ruby?reject is the inverse of select. While select keeps elements where the block is true, reject removes them.Click to reveal answer
beginner
Example: What is the result of
[1, 2, 3, 4].reject { |n| n.even? }?The result is
[1, 3] because reject removes even numbers (2 and 4).Click to reveal answer
intermediate
Can
reject be used with hashes in Ruby?Yes,
reject works with hashes and returns a new hash excluding key-value pairs where the block returns true.Click to reveal answer
intermediate
What happens if no block is given to
reject?If no block is given,
reject returns an Enumerator, allowing chaining or delayed execution.Click to reveal answer
What does
reject do in Ruby?✗ Incorrect
reject removes elements for which the block returns true, filtering them out.What is the output of
[5, 6, 7].reject { |n| n > 6 }?✗ Incorrect
Elements greater than 6 (which is 7) are rejected, so the result is [5, 6].
Which method is the inverse of
reject?✗ Incorrect
select keeps elements where the block returns true, opposite of reject.What does
reject return if no block is given?✗ Incorrect
Without a block,
reject returns an Enumerator for chaining.Can
reject be used on a hash?✗ Incorrect
reject works on hashes and returns a new hash excluding matching pairs.Explain how the
reject method works in Ruby and how it differs from select.Think about what elements stay or go based on the block's result.
You got /3 concepts.
Write a Ruby code example using
reject to remove all odd numbers from an array.Use n.odd? inside the block.
You got /4 concepts.