0
0
Rubyprogramming~10 mins

Array modification (push, pop, shift, unshift) in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Array modification (push, pop, shift, unshift)
Start with array
Choose operation
Add end
Array updated
End
Start with an array, pick one of four operations: push adds to end, pop removes from end, shift removes from start, unshift adds to start, then array updates.
Execution Sample
Ruby
arr = [1, 2, 3]
arr.push(4)
arr.pop
arr.shift
arr.unshift(0)
Modify array by adding/removing elements at start or end using push, pop, shift, and unshift.
Execution Table
StepOperationArray BeforeActionArray After
1Initialize[]Set arr = [1, 2, 3][1, 2, 3]
2push(4)[1, 2, 3]Add 4 at end[1, 2, 3, 4]
3pop[1, 2, 3, 4]Remove last element (4)[1, 2, 3]
4shift[1, 2, 3]Remove first element (1)[2, 3]
5unshift(0)[2, 3]Add 0 at start[0, 2, 3]
💡 All operations done, final array is [0, 2, 3]
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
arr[][1, 2, 3][1, 2, 3, 4][1, 2, 3][2, 3][0, 2, 3]
Key Moments - 3 Insights
Why does pop remove the last element, but shift removes the first?
Pop always removes from the end of the array (see step 3), while shift removes from the start (see step 4). They target opposite ends.
What happens to the array length after unshift compared to push?
Both add one element, increasing length by 1. Push adds at the end (step 2), unshift adds at the start (step 5).
If the array is empty, what happens when pop or shift is called?
Pop or shift on an empty array returns nil and does not change the array size.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the array after step 3 (pop)?
A[1, 2, 3]
B[1, 2, 3, 4]
C[2, 3]
D[0, 2, 3]
💡 Hint
Check the 'Array After' column for step 3 in the execution_table.
At which step is the element '1' removed from the array?
AStep 2 (push)
BStep 4 (shift)
CStep 3 (pop)
DStep 5 (unshift)
💡 Hint
Look at the 'Action' column in the execution_table to see when '1' is removed.
If we replace unshift(0) with unshift(5), what will be the final array after step 5?
A[0, 2, 3]
B[2, 3, 5]
C[5, 2, 3]
D[1, 2, 3]
💡 Hint
Unshift adds the given element at the start of the array (see step 5).
Concept Snapshot
Array modification methods:
- push(value): add value at end
- pop: remove last element
- shift: remove first element
- unshift(value): add value at start
Each changes array size accordingly.
Full Transcript
This visual trace shows how Ruby arrays change with push, pop, shift, and unshift. We start with arr = [1, 2, 3]. Push(4) adds 4 at the end making [1, 2, 3, 4]. Pop removes last element 4 returning to [1, 2, 3]. Shift removes first element 1 resulting in [2, 3]. Unshift(0) adds 0 at the start, finalizing [0, 2, 3]. Each step updates the array and changes its size. Pop and shift remove elements from opposite ends. Push and unshift add elements at opposite ends. If pop or shift is called on an empty array, it returns nil and array stays empty.