0
0
Rubyprogramming~15 mins

Array modification (push, pop, shift, unshift) in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Array modification (push, pop, shift, unshift)
📖 Scenario: You are managing a simple to-do list app. You need to add and remove tasks from your list using basic array methods.
🎯 Goal: Build a Ruby program that modifies an array of tasks by adding and removing items using push, pop, shift, and unshift.
📋 What You'll Learn
Create an array called tasks with initial tasks
Add a new task to the end of the tasks array using push
Remove the last task from the tasks array using pop
Remove the first task from the tasks array using shift
Add a new task to the beginning of the tasks array using unshift
Print the final tasks array
💡 Why This Matters
🌍 Real World
Managing lists of tasks or items is common in apps like to-do lists, shopping lists, or event planners.
💼 Career
Understanding how to modify arrays is essential for many programming jobs, especially those involving data manipulation and user interfaces.
Progress0 / 4 steps
1
Create the initial tasks array
Create an array called tasks with these exact strings: 'wash dishes', 'do laundry', 'buy groceries'
Ruby
Need a hint?

Use square brackets [] to create an array and separate items with commas.

2
Add a new task to the end
Use push to add the string 'clean room' to the end of the tasks array
Ruby
Need a hint?

Use tasks.push('clean room') to add the task at the end.

3
Remove the last task
Use pop to remove the last task from the tasks array
Ruby
Need a hint?

Use tasks.pop to remove the last item from the array.

4
Remove the first task and add a new one at the start
Use shift to remove the first task from tasks, then use unshift to add the string 'pay bills' to the beginning of tasks. Finally, print the tasks array.
Ruby
Need a hint?

Use tasks.shift to remove the first item and tasks.unshift('pay bills') to add a new item at the start. Use print(tasks) to show the final array.