0
0
Rubyprogramming~5 mins

Reject for inverse filtering in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AKeeps elements where the block returns true
BRemoves elements where the block returns true
CSorts elements in ascending order
DReturns the first element
What is the output of [5, 6, 7].reject { |n| n > 6 }?
A[5, 6]
B[7]
C[5, 7]
D[6, 7]
Which method is the inverse of reject?
Aeach
Bmap
Creduce
Dselect
What does reject return if no block is given?
Anil
BAn empty array
CAn Enumerator
DRaises an error
Can reject be used on a hash?
AYes, it returns a hash excluding pairs where block is true
BNo, it only works on arrays
CYes, but returns an array
DNo, it raises an error
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.