0
0
Rubyprogramming~10 mins

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

Choose your learning style9 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.