We use array modification methods to add or remove items from a list easily. This helps us change the list without making a new one.
Array modification (push, pop, shift, unshift) in Ruby
class Array # Add item to the end def push(item) self[self.length] = item self end # Remove last item def pop return nil if self.empty? last_item = self[-1] self.delete_at(-1) last_item end # Remove first item def shift return nil if self.empty? first_item = self[0] self.delete_at(0) first_item end # Add item to the front def unshift(item) self.insert(0, item) self end end
These methods change the original array directly.
push and unshift add items; pop and shift remove items.
my_array = [] my_array.push(10) # my_array is now [10]
my_array = [5] removed = my_array.pop # removed is 5 # my_array is now []
my_array = [1, 2, 3] first_item = my_array.shift # first_item is 1 # my_array is now [2, 3]
my_array = [2, 3] my_array.unshift(1) # my_array is now [1, 2, 3]
This program shows how to add and remove items from an array using push, pop, shift, and unshift. It prints the array before and after each change.
my_array = ["apple", "banana", "cherry"] puts "Original array: #{my_array}" # Add to end my_array.push("date") puts "After push('date'): #{my_array}" # Remove last removed_last = my_array.pop puts "After pop(): #{my_array}, removed: #{removed_last}" # Remove first removed_first = my_array.shift puts "After shift(): #{my_array}, removed: #{removed_first}" # Add to front my_array.unshift("apricot") puts "After unshift('apricot'): #{my_array}"
Time complexity: push and pop are fast (O(1)), shift and unshift can be slower (O(n)) because they move many items.
Space complexity is usually O(1) since changes happen in place.
Common mistake: expecting shift and unshift to be as fast as push and pop, but they move all elements.
Use push/pop when working with the end of the list, and shift/unshift when working with the start.
push adds an item to the end of an array.
pop removes the last item and returns it.
shift removes the first item and returns it.
unshift adds an item to the front of an array.
Practice
Solution
Step 1: Understand the purpose of
Thepushpushmethod adds an element to the end of an array.Step 2: Compare with other methods
popremoves the last element,shiftremoves the first, andunshiftadds to the front.Final Answer:
push -> Option CQuick Check:
push adds to end [OK]
- Confusing push with pop
- Mixing shift and unshift
- Thinking pop adds elements
arr in Ruby?Solution
Step 1: Identify method to remove first element
Theshiftmethod removes and returns the first element of an array.Step 2: Check syntax correctness
Callingarr.shift()correctly removes the first element.Final Answer:
arr.shift() -> Option AQuick Check:
shift removes first element [OK]
- Using pop to remove first element
- Calling unshift to remove elements
- Forgetting parentheses
arr = [1, 2, 3] arr.push(4) arr.shift p arr
Solution
Step 1: Execute
Adding 4 to the end changes array from [1, 2, 3] to [1, 2, 3, 4].push(4)on arrayStep 2: Execute
Removing first element (1) results in [2, 3, 4].shiftto remove first elementFinal Answer:
[2, 3, 4] -> Option DQuick Check:
push adds end, shift removes front = [2, 3, 4] [OK]
- Ignoring shift effect
- Thinking push adds front
- Confusing pop with shift
nums and then remove the last element. What is the error?nums = [1, 2, 3] nums.unshift(0) nums.pop()
Solution
Step 1: Understand
This adds 0 to the front, changing [1, 2, 3] to [0, 1, 2, 3].unshift(0)effectStep 2: Understand
This removes the last element (3), resulting in [0, 1, 2].pop()effectFinal Answer:
No error; code works correctly -> Option AQuick Check:
unshift adds front, pop removes last = correct [OK]
- Thinking pop removes first element
- Believing unshift does not add
- Swapping order of operations unnecessarily
data = [10, 20, 30], which sequence of method calls will result in [5, 10, 20]?Solution
Step 1: Apply
Array changes from [10, 20, 30] to [5, 10, 20, 30].unshift(5)to add 5 at frontStep 2: Apply
Removing last element (30) results in [5, 10, 20].pop()to remove last elementFinal Answer:
data.unshift(5).pop() -> Option BQuick Check:
unshift adds front, pop removes last = [5, 10, 20] [OK]
- Using push instead of unshift
- Removing first element instead of last
- Chaining methods incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
