Menu
Coddy logo textTech

Modifying Arrays

Part of the Fundamentals section of Coddy's Ruby journey — lesson 60 of 88.

Now that you know how to access array elements, let's learn how to change them. You can modify any element by assigning a new value to its index:

fruits = ["apple", "banana", "cherry"]
fruits[1] = "mango"
puts fruits  # Outputs: apple, mango, cherry

To add elements to an array, use the push method or the shovel operator <<. Both add elements to the end:

colors = ["red", "blue"]
colors.push("green")
colors << "yellow"
puts colors  # Outputs: red, blue, green, yellow

To remove elements, Ruby provides several options. The pop method removes and returns the last element, while shift removes the first:

numbers = [1, 2, 3, 4]
numbers.pop    # Removes 4
numbers.shift  # Removes 1
puts numbers   # Outputs: 2, 3

You can also remove a specific value using delete:

letters = ["a", "b", "c", "b"]
letters.delete("b")
puts letters  # Outputs: a, c

Note that delete removes all occurrences of the specified value from the array.

challenge icon

Challenge

Easy

You are provided with the following array:

tasks = ["email", "meeting", "lunch", "report", "call"]

Read three inputs:

  1. An index (integer) - the position to modify
  2. A new value (string) - the replacement value for that position
  3. A value to delete (string) - an element to remove from the array

Perform the following operations in order:

  1. Replace the element at the given index with the new value
  2. Add "review" to the end of the array using push
  3. Add "planning" to the end of the array using the shovel operator <<
  4. Remove the last element using pop
  5. Remove the first element using shift
  6. Delete all occurrences of the specified value using delete

Print the final array using puts.

For example, if the inputs are 2, break, and call, the output should be:

meeting
break
report
review

If the inputs are 0, standup, and report, the output should be:

meeting
lunch
call
review

If the inputs are 4, demo, and lunch, the output should be:

meeting
report
demo
review

Cheat sheet

To modify an array element, assign a new value to its index:

fruits = ["apple", "banana", "cherry"]
fruits[1] = "mango"
puts fruits  # Outputs: apple, mango, cherry

To add elements to the end of an array, use push or the shovel operator <<:

colors = ["red", "blue"]
colors.push("green")
colors << "yellow"
puts colors  # Outputs: red, blue, green, yellow

To remove elements from an array:

  • pop removes and returns the last element
  • shift removes and returns the first element
  • delete removes all occurrences of a specific value
numbers = [1, 2, 3, 4]
numbers.pop    # Removes 4
numbers.shift  # Removes 1
puts numbers   # Outputs: 2, 3

letters = ["a", "b", "c", "b"]
letters.delete("b")
puts letters  # Outputs: a, c

Try it yourself

# Initial array
tasks = ["email", "meeting", "lunch", "report", "call"]

# Read inputs
index = gets.chomp.to_i
new_value = gets.chomp
value_to_delete = gets.chomp

# TODO: Write your code below
# 1. Replace the element at the given index with the new value
# 2. Add "review" to the end using push
# 3. Add "planning" to the end using the shovel operator <<
# 4. Remove the last element using pop
# 5. Remove the first element using shift
# 6. Delete all occurrences of the specified value using delete

# Print the final array
puts tasks
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals