Bird
0
0

In Ruby, what is the primary purpose of the reject method when applied to an array?

easy📝 Conceptual Q1 of 15
Ruby - Enumerable and Collection Processing
In Ruby, what is the primary purpose of the reject method when applied to an array?
ATo modify the original array by removing elements that meet a condition
BTo return a new array excluding elements that meet a specified condition
CTo select only the elements that meet a specified condition
DTo sort the array in descending order
Step-by-Step Solution
Solution:
  1. Step 1: Understand reject's behavior

    The reject method returns a new array excluding elements for which the block returns true.
  2. Step 2: Differentiate from select

    Unlike select, which keeps elements meeting the condition, reject excludes them.
  3. Final Answer:

    To return a new array excluding elements that meet a specified condition -> Option B
  4. Quick Check:

    Reject excludes matching elements [OK]
Quick Trick: Reject excludes elements matching condition [OK]
Common Mistakes:
  • Confusing reject with select
  • Assuming reject modifies the original array
  • Thinking reject sorts the array

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes