Bummer! This is just a preview. You need to be signed in with a Treehouse account to view the entire video.
Removing Items From Arrays3:43 with Jason Seifer
So far while working with arrays, we've learned how to add and access the items inside. Now it's time to learn how to remove items in arrays.
Let's assume we have the following array:
array = ["milk", "eggs", "bread", "ice cream", "carrots", "potatoes"]
To access and remove the last item in the array, we can use the
last_item = array.pop
last_item variable will now contain the string
potatoes. The array now looks like this:
["milk", "eggs", "bread", "ice cream", "carrots"]
To access and remove the first item in the array, we use the
first_item = array.shift
first_item variable now contains the string
milk. The array now looks like this:
[ "eggs", "bread", "ice cream", "carrots" ]
We can use the
drop method to take out a number of items from an array. The
drop method's argument is the number of items to remove from the front of the array.
ice_cream_carrots = array.drop(2)
Similar to the
unshift method, which adds to the beginning of an array, the
shift method removes from the beginning of an array:
item = array.shift
item variable now contains the string "eggs" and our
array looks like this:
[ "bread", "ice cream", "carrots" ]
Let's add "potatoes" to the end of our array:
array now contains:
["bread", "ice cream", "carrots", "potatoes"]
slice method takes two arguments and removes and returns items from the array. The first argument is the index in the array, and the second argument is the number of items:
first_three_items = array.slice(0, 3)
first_three_items variable contains the following array:
["bread", "ice cream", "carrots"]
array variable is now a single element:
Now that we can add things to arrays and get things back out of arrays,
wouldn’t it be useful if we could also remove things from arrays?
Let’s take a look at how that works now using workspaces.
Okay, so let launch irb again inside of a Ruby workspace.
And we'll set up our grocery list array.
So we'll give this a few different items here.
All right, now if we want to get to the last item in this array,
let's go ahead and assign that to a variable called last_item.
And we'll use the pop method, which returns the last item in the array,
and removes it from the original array.
That's the big difference between the last method and the pop method.
So if we look at the last item here, we'll say grocery_list.pop.
Now our variable last_item has the string potatoes, but
if we look in our grocery_list this time, it is now lacking potatoes.
Similarly, there's another method called shift, which returns the first item.
So we'll look at the grocery_list array here.
And we'll call the shift method on it.
Now, it we look at our first item, it contains the string milk.
And if we take a look at our grocery_list,
it now only has four items in it, eggs, bread, ice cream, and pie.
So we've already done some shopping here.
Now let's say we wanted to take two items off of this grocery_list.
Let's create a variable called drop_two_items.
And we'll say grocery list, the method drop, and the number 2.
Now let's go ahead and look at our drop_two_items variable.
It contains ice cream and pie, the last two items in the array.
Now let's take a look at our grocery_list.
The grocery list was not modified by the drop method and
still has everything inside of it.
Gonna clear my screen here.
Let's take a look at our grocery_list.
Now here's another cool thing.
If we want to add onto the beginning of the array, just like
we took from the beginning of the array earlier, we could use the unshift method.
So, let's say, grocery_list.unshift, and we'll give that milk.
Now our grocery list has milk on the front of it.
And let's go ahead and put potatoes back on there.
All right, now we have a pretty decent grocery list to work with.
I'm going to clear my screen again, and that's our grocery list.
And let's say we wanted the first three items from this grocery list.
We could use the slice method.
So let's say, first_three_items = grocery_list.slice.
Now we give it the index, which is 0, and the number of items we want, which is 3.
Now first_three_items contains milk, eggs, and bread,
and our grocery_list was unmodified by the slice command.
Now we could give it any index we wanted.
So we could say new_list is grocery_list.slice and then any index.
Let’s give it the index of 2.
And say we only want 2 items, and that's gonna be bread and ice cream.
Now that's what our new list contains as well.
Try removing things from arrays now on your own, using Workspaces.
You need to sign up for Treehouse in order to download course files.Sign up