Bird
Raised Fist0
Rubyprogramming~10 mins

Reject for inverse filtering in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Reject for inverse filtering
Start with array
↓
Apply inverse filter condition
↓
Reject elements matching condition
↓
Return filtered array
This flow shows how elements are checked against a condition and rejected if they match, returning the rest.
Execution Sample
Ruby
numbers = [1, 2, 3, 4, 5]
filtered = numbers.reject { |n| n % 2 == 0 }
puts filtered.inspect
This code removes even numbers from the array using reject with an inverse condition.
Execution Table
StepElement (n)Condition (n % 2 == 0)Reject? (True=Remove)Resulting Array
111 % 2 == 0 => falseNo[1]
222 % 2 == 0 => trueYes[1]
333 % 2 == 0 => falseNo[1, 3]
444 % 2 == 0 => trueYes[1, 3]
555 % 2 == 0 => falseNo[1, 3, 5]
End---Final array: [1, 3, 5]
💡 All elements checked; evens rejected; final array contains only odd numbers.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
numbers[1, 2, 3, 4, 5][1, 2, 3, 4, 5][1, 2, 3, 4, 5][1, 2, 3, 4, 5][1, 2, 3, 4, 5][1, 2, 3, 4, 5][1, 2, 3, 4, 5]
filtered[][1][1][1, 3][1, 3][1, 3, 5][1, 3, 5]
Key Moments - 2 Insights
Why does reject remove elements where the condition is true, not false?
Reject removes elements for which the block returns true, so when the condition is true (like n % 2 == 0), those elements are removed, as shown in steps 2 and 4 in the execution_table.
What happens if the condition is always false?
If the condition is always false, reject removes nothing and the resulting array is the same as the original, as no elements meet the reject condition.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the filtered array after step 3?
A[1]
B[1, 3]
C[1, 2, 3]
D[3]
💡 Hint
Check the 'Resulting Array' column at step 3 in the execution_table.
At which step does the condition become true for the first time?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Condition' column in the execution_table to find the first true condition.
If we change the condition to n % 2 != 0, what would happen to the filtered array?
AIt would be empty
BIt would contain only odd numbers
CIt would contain only even numbers
DIt would be the same as the original
💡 Hint
Reject removes elements where the condition is true; changing to n % 2 != 0 rejects odd numbers.
Concept Snapshot
reject { |x| condition } removes elements where condition is true
Used to filter out unwanted items
Returns a new array without rejected elements
Opposite of select which keeps elements matching condition
Common for inverse filtering in Ruby
Full Transcript
This example shows how Ruby's reject method works by removing elements from an array when a condition is true. We start with an array of numbers. Each number is checked: if it is even (n % 2 == 0), reject removes it. Odd numbers remain. The execution table traces each step, showing which elements are removed and the array's state after each step. Key points include understanding that reject removes elements when the block returns true, and if the condition is always false, no elements are removed. Changing the condition changes which elements are rejected. This is a simple way to filter arrays by excluding unwanted items.

Practice

(1/5)
1. What does the Ruby method reject do when called on an array?
easy
A. It sorts the array in reverse order.
B. It modifies the original array to keep only elements matching the condition.
C. It returns the first element that matches the condition.
D. It returns a new array excluding elements that match the given condition.

Solution

  1. Step 1: Understand the purpose of reject

    The reject method filters elements by removing those that satisfy the condition.
  2. Step 2: Compare with other methods

    Unlike select, which keeps matching elements, reject excludes them and returns a new array.
  3. Final Answer:

    It returns a new array excluding elements that match the given condition. -> Option D
  4. Quick Check:

    reject excludes matching elements = A [OK]
Hint: Reject removes items matching condition, returning a new array [OK]
Common Mistakes:
  • Thinking reject modifies the original array
  • Confusing reject with select
  • Assuming reject returns a single element
2. Which of the following is the correct syntax to use reject on an array nums to remove even numbers?
easy
A. nums.reject { |n| n % 2 == 0 }
B. nums.reject(n % 2 == 0)
C. nums.reject => n { n % 2 == 0 }
D. nums.reject do n; n % 2 == 0 end

Solution

  1. Step 1: Identify correct block syntax for reject

    The reject method requires a block with { |var| condition } or do |var| ... end.
  2. Step 2: Check each option's syntax

    nums.reject { |n| n % 2 == 0 } uses correct block syntax with braces and block variable. nums.reject(n % 2 == 0) misses block. nums.reject => n { n % 2 == 0 } uses invalid syntax. nums.reject do n; n % 2 == 0 end misses block variable in do syntax.
  3. Final Answer:

    nums.reject { |n| n % 2 == 0 } -> Option A
  4. Quick Check:

    Correct block syntax with |var| = D [OK]
Hint: Use braces with |var| for reject blocks [OK]
Common Mistakes:
  • Omitting block variable in block
  • Passing condition as method argument
  • Using incorrect block syntax
3. What is the output of this Ruby code?
arr = [1, 2, 3, 4, 5]
result = arr.reject { |x| x > 3 }
puts result.inspect
medium
A. [1, 2, 3]
B. [4, 5]
C. [1, 2, 3, 4, 5]
D. []

Solution

  1. Step 1: Understand the reject condition

    The block removes elements where x > 3 is true, so numbers 4 and 5 are removed.
  2. Step 2: Determine remaining elements

    Elements 1, 2, and 3 remain because they do not satisfy the condition.
  3. Final Answer:

    [1, 2, 3] -> Option A
  4. Quick Check:

    reject removes >3, so [1,2,3] remain = A [OK]
Hint: Reject removes matching items, so keep others [OK]
Common Mistakes:
  • Confusing reject with select
  • Expecting removed elements as output
  • Ignoring block condition logic
4. The following code is intended to remove all nil values from the array data. What is the error?
data = [1, nil, 2, nil, 3]
clean = data.reject do |x|
  x = nil
end
puts clean.inspect
medium
A. The block variable is missing in the block.
B. The reject method cannot remove nil values.
C. The block uses assignment (=) instead of comparison (==).
D. The code should use select instead of reject.

Solution

  1. Step 1: Identify the block logic error

    The block uses x = nil which assigns nil to x instead of checking if x equals nil.
  2. Step 2: Correct the comparison operator

    It should use x == nil to test if x is nil for rejection.
  3. Final Answer:

    The block uses assignment (=) instead of comparison (==). -> Option C
  4. Quick Check:

    Use == for comparison inside reject blocks [OK]
Hint: Use == for comparison inside reject blocks [OK]
Common Mistakes:
  • Using = instead of == in block condition
  • Thinking reject modifies original array
  • Confusing reject with select
5. Given a hash scores = { alice: 90, bob: 55, carol: 75, dave: 40 }, how can you use reject to create a new hash excluding all entries with scores below 60?
hard
A. scores.reject { |name| name < 60 }
B. scores.reject { |name, score| score < 60 }
C. scores.reject { |score| score < 60 }
D. scores.reject { |name, score| name < 60 }

Solution

  1. Step 1: Understand hash reject block parameters

    When rejecting from a hash, the block receives two parameters: key and value.
  2. Step 2: Apply condition on values

    We want to reject entries where the score (value) is less than 60, so the block should check score < 60.
  3. Step 3: Verify options

    scores.reject { |name, score| score < 60 } correctly uses both key and value parameters and checks the score. The other options either use only one block parameter or check the key instead of the value.
  4. Final Answer:

    scores.reject { |name, score| score < 60 } -> Option B
  5. Quick Check:

    Reject hash entries by value condition = B [OK]
Hint: Use two block params for hash reject: |key, value| [OK]
Common Mistakes:
  • Using only one block parameter for hash reject
  • Checking key instead of value
  • Confusing reject with select for filtering
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard