0
0
Rubyprogramming~10 mins

Reduce/inject for accumulation in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Reduce/inject for accumulation
Start with initial accumulator
Take next element from collection
Apply block: accumulator + element
Update accumulator with result
More elements?
YesRepeat
No
Return accumulator
The reduce/inject method starts with an initial value and combines it with each element of a collection step-by-step, updating the accumulator until all elements are processed.
Execution Sample
Ruby
numbers = [1, 2, 3, 4]
result = numbers.reduce(0) { |sum, n| sum + n }
puts result
This code sums all numbers in the array using reduce, starting from 0.
Execution Table
StepAccumulator (sum)Current Element (n)Block OperationNew Accumulator Value
1010 + 11
2121 + 23
3333 + 36
4646 + 410
End10--All elements processed, return 10
💡 All elements processed, reduce returns final accumulator value 10
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
sum01361010
n-1234-
Key Moments - 3 Insights
Why does the accumulator start at 0 and not the first element?
Because we explicitly gave 0 as the initial value in reduce(0). This means sum starts at 0, not the first array element. See execution_table step 1 where sum is 0 before adding 1.
What happens if we don't provide an initial value to reduce?
Then reduce uses the first element as the initial accumulator and starts from the second element. This changes how the block runs and the initial sum value. Our example shows the version with an explicit initial value.
How does the block update the accumulator?
At each step, the block takes the current accumulator and the current element, adds them, and returns the new accumulator. This is shown in the 'Block Operation' and 'New Accumulator Value' columns in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the accumulator value after processing the second element?
A1
B3
C2
D0
💡 Hint
Check the 'After 2' column in variable_tracker or step 2 row in execution_table.
At which step does the accumulator first become 6?
AStep 3
BStep 1
CStep 4
DEnd
💡 Hint
Look at the 'New Accumulator Value' column in execution_table for when sum is 6.
If the initial value was changed from 0 to 10, what would be the final accumulator value?
A30
B10
C20
D14
💡 Hint
Sum all elements (1+2+3+4=10) plus initial 10 equals 20.
Concept Snapshot
reduce/inject combines elements into one value
Syntax: collection.reduce(initial) { |acc, elem| ... }
Starts with initial accumulator
Block updates accumulator with each element
Returns final accumulator after all elements
Full Transcript
This visual trace shows how Ruby's reduce (also called inject) method works for accumulation. We start with an initial value (0) as the accumulator. Then for each element in the array, the block adds the element to the accumulator and returns the new sum. This repeats until all elements are processed. The final accumulator value is returned. The execution table shows each step's accumulator, current element, operation, and new accumulator. The variable tracker shows how sum and current element change over time. Key moments clarify why the initial value matters and how the block updates the accumulator. The quiz tests understanding of accumulator values at different steps and effects of changing the initial value.