Step 17: Push/Pop/Shift/Unshift


Arrays also have some handy methods for adding and removing items from the beginning and end of an array.

travel_necessities = ["camera", "sunglasses", "first aid kit", "carrier pigeon"]
bird = travel_necessities.pop
puts(travel_necessities)


camera = travel_necessities.shift
puts("_____________________")
puts(travel_necessities)


travel_necessities.push(camera)
travel_necessities.unshift(bird)
puts("_____________________")
puts(travel_necessities)

As you can see .pop will remove the last item of an array, .shift will remove the first item, .push will put an item at the end, and .unshift will put an item at the beginning.