Bird
Raised Fist0
Rubyprogramming~5 mins

Array modification (push, pop, shift, unshift) in Ruby

Choose your learning style10 modes available
Introduction

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.

When you want to add a new item to the end of a list, like adding a new song to a playlist.
When you want to remove the last item from a list, like taking the last book off a shelf.
When you want to remove the first item from a list, like serving the first customer in line.
When you want to add a new item to the front of a list, like putting a new task at the top of your to-do list.
Syntax
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.

Examples
Adding an item to an empty array using push.
Ruby
my_array = []
my_array.push(10)
# my_array is now [10]
Removing the only item with pop results in an empty array.
Ruby
my_array = [5]
removed = my_array.pop
# removed is 5
# my_array is now []
Removing the first item shifts the rest left.
Ruby
my_array = [1, 2, 3]
first_item = my_array.shift
# first_item is 1
# my_array is now [2, 3]
Adding an item to the front with unshift.
Ruby
my_array = [2, 3]
my_array.unshift(1)
# my_array is now [1, 2, 3]
Sample Program

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.

Ruby
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}"
OutputSuccess
Important Notes

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.

Summary

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

(1/5)
1. Which Ruby array method adds an element to the end of the array?
easy
A. shift
B. pop
C. push
D. unshift

Solution

  1. Step 1: Understand the purpose of push

    The push method adds an element to the end of an array.
  2. Step 2: Compare with other methods

    pop removes the last element, shift removes the first, and unshift adds to the front.
  3. Final Answer:

    push -> Option C
  4. Quick Check:

    push adds to end [OK]
Hint: Remember: push = add end, unshift = add front [OK]
Common Mistakes:
  • Confusing push with pop
  • Mixing shift and unshift
  • Thinking pop adds elements
2. Which of the following is the correct syntax to remove the first element from an array arr in Ruby?
easy
A. arr.shift()
B. arr.pop()
C. arr.unshift()
D. arr.push()

Solution

  1. Step 1: Identify method to remove first element

    The shift method removes and returns the first element of an array.
  2. Step 2: Check syntax correctness

    Calling arr.shift() correctly removes the first element.
  3. Final Answer:

    arr.shift() -> Option A
  4. Quick Check:

    shift removes first element [OK]
Hint: shift removes first, pop removes last [OK]
Common Mistakes:
  • Using pop to remove first element
  • Calling unshift to remove elements
  • Forgetting parentheses
3. What will be the output of the following Ruby code?
arr = [1, 2, 3]
arr.push(4)
arr.shift
p arr
medium
A. [1, 2, 3]
B. [1, 2, 3, 4]
C. [4, 1, 2, 3]
D. [2, 3, 4]

Solution

  1. Step 1: Execute push(4) on array

    Adding 4 to the end changes array from [1, 2, 3] to [1, 2, 3, 4].
  2. Step 2: Execute shift to remove first element

    Removing first element (1) results in [2, 3, 4].
  3. Final Answer:

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

    push adds end, shift removes front = [2, 3, 4] [OK]
Hint: push adds end, shift removes front, watch order [OK]
Common Mistakes:
  • Ignoring shift effect
  • Thinking push adds front
  • Confusing pop with shift
4. The following Ruby code is intended to add 0 to the front of the array nums and then remove the last element. What is the error?
nums = [1, 2, 3]
nums.unshift(0)
nums.pop()
medium
A. No error; code works correctly
B. pop() should be called before unshift()
C. unshift() does not add elements to array
D. pop() removes the first element, not the last

Solution

  1. Step 1: Understand unshift(0) effect

    This adds 0 to the front, changing [1, 2, 3] to [0, 1, 2, 3].
  2. Step 2: Understand pop() effect

    This removes the last element (3), resulting in [0, 1, 2].
  3. Final Answer:

    No error; code works correctly -> Option A
  4. Quick Check:

    unshift adds front, pop removes last = correct [OK]
Hint: unshift adds front, pop removes last, order matters [OK]
Common Mistakes:
  • Thinking pop removes first element
  • Believing unshift does not add
  • Swapping order of operations unnecessarily
5. Given the array data = [10, 20, 30], which sequence of method calls will result in [5, 10, 20]?
hard
A. data.push(5).shift()
B. data.unshift(5).pop()
C. data.shift().unshift(5)
D. data.pop().unshift(5)

Solution

  1. Step 1: Apply unshift(5) to add 5 at front

    Array changes from [10, 20, 30] to [5, 10, 20, 30].
  2. Step 2: Apply pop() to remove last element

    Removing last element (30) results in [5, 10, 20].
  3. Final Answer:

    data.unshift(5).pop() -> Option B
  4. Quick Check:

    unshift adds front, pop removes last = [5, 10, 20] [OK]
Hint: Add front with unshift, then remove last with pop [OK]
Common Mistakes:
  • Using push instead of unshift
  • Removing first element instead of last
  • Chaining methods incorrectly
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