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, cherryTo 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, yellowTo 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, 3You can also remove a specific value using delete:
letters = ["a", "b", "c", "b"]
letters.delete("b")
puts letters # Outputs: a, cNote that delete removes all occurrences of the specified value from the array.
Challenge
EasyYou are provided with the following array:
tasks = ["email", "meeting", "lunch", "report", "call"]Read three inputs:
- An index (integer) - the position to modify
- A new value (string) - the replacement value for that position
- A value to delete (string) - an element to remove from the array
Perform the following operations in order:
- Replace the element at the given index with the new value
- Add
"review"to the end of the array usingpush - Add
"planning"to the end of the array using the shovel operator<< - Remove the last element using
pop - Remove the first element using
shift - 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
reviewIf the inputs are 0, standup, and report, the output should be:
meeting
lunch
call
reviewIf the inputs are 4, demo, and lunch, the output should be:
meeting
report
demo
reviewCheat 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, cherryTo 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, yellowTo remove elements from an array:
popremoves and returns the last elementshiftremoves and returns the first elementdeleteremoves 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, cTry 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 tasksThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 42Variables and Data Types
Numbers and VariablesString Data TypeBoolean Data TypeSymbol Data TypeChecking Data TypesNaming ConventionsRecap - Variable Creation8Loops
For Loop with RangesWhile LoopBreakNextRecap - FactorialTimes LoopUntil LoopNested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators6Basic IO
Output with putsOutput with print and pOutput With VariablesInput with getsChomp MethodType ConversionRecap - Age CalculatorRecap - True or False