1 00:00:00,660 --> 00:00:04,870 Now that we can add things to arrays and get things back out of arrays, 2 00:00:04,870 --> 00:00:09,480 wouldn’t it be useful if we could also remove things from arrays? 3 00:00:09,480 --> 00:00:13,420 Let’s take a look at how that works now using workspaces. 4 00:00:13,420 --> 00:00:17,310 Okay, so let's launch irb again inside of a Ruby workspace. 5 00:00:17,310 --> 00:00:20,719 And we'll set up our grocery_list array. 6 00:00:20,719 --> 00:00:22,619 So we'll give this a few different items here. 7 00:00:30,319 --> 00:00:34,797 All right, now if we want to get to the last item in this array, 8 00:00:34,797 --> 00:00:40,120 let's go ahead and assign that to a variable called last_item. 9 00:00:40,120 --> 00:00:45,470 And we'll use the pop method, which returns the last item in the array, 10 00:00:45,470 --> 00:00:48,450 and removes it from the original array. 11 00:00:48,450 --> 00:00:52,170 That's the big difference between the last method and the pop method. 12 00:00:53,440 --> 00:00:58,790 So if we look at the last item here, we'll say grocery_list.pop. 13 00:00:58,790 --> 00:01:03,330 Now our variable last_item has the string "potatoes", but 14 00:01:03,330 --> 00:01:07,580 if we look in our grocery_list this time, it is now lacking "potatoes". 15 00:01:08,960 --> 00:01:13,410 Similarly, there's another method called shift, which returns the first item. 16 00:01:14,630 --> 00:01:17,800 So we'll look at the grocery_list array here. 17 00:01:17,800 --> 00:01:20,610 And we'll call the shift method on it. 18 00:01:20,610 --> 00:01:24,080 Now, it we look at our first item, it contains the string "milk". 19 00:01:25,520 --> 00:01:27,370 And if we take a look at our grocery_list, 20 00:01:27,370 --> 00:01:32,480 it now only has four items in it, "eggs", "bread", "ice cream", and "pie". 21 00:01:32,480 --> 00:01:34,130 So we've already done some shopping here. 22 00:01:35,290 --> 00:01:39,172 Now let's say we wanted to take two items off of this grocery_list. 23 00:01:40,510 --> 00:01:43,589 Let's create a variable called drop_two_items. 24 00:01:45,009 --> 00:01:50,500 And we'll say grocery list, the method drop, and the number 2. 25 00:01:50,500 --> 00:01:54,820 Now let's go ahead and look at our drop_two_items variable. 26 00:01:54,820 --> 00:01:59,570 It contains "ice cream" and "pie", the last two items in the array. 27 00:02:00,690 --> 00:02:02,235 Now let's take a look at our grocery_list. 28 00:02:03,260 --> 00:02:07,530 The grocery list was not modified by the drop method and 29 00:02:07,530 --> 00:02:09,560 still has everything inside of it. 30 00:02:10,560 --> 00:02:12,280 Gonna clear my screen here. 31 00:02:12,280 --> 00:02:13,645 Let's take a look at our grocery_list. 32 00:02:14,840 --> 00:02:16,250 Now here's another cool thing. 33 00:02:16,250 --> 00:02:19,960 If we want to add onto the beginning of the array, just like 34 00:02:19,960 --> 00:02:25,200 we took from the beginning of the array earlier, we could use the unshift method. 35 00:02:25,200 --> 00:02:29,970 So, let's say, grocery_list.unshift, and we'll give that "milk". 36 00:02:31,160 --> 00:02:33,650 Now our grocery_list has "milk" on the front of it. 37 00:02:36,170 --> 00:02:38,570 And let's go ahead and put "potatoes" back on there. 38 00:02:40,010 --> 00:02:42,829 All right, now we have a pretty decent grocery list to work with. 39 00:02:42,829 --> 00:02:47,889 I'm going to clear my screen again, and that's our grocery list. 40 00:02:47,889 --> 00:02:52,589 And let's say we wanted the first three items from this grocery list. 41 00:02:52,589 --> 00:02:54,369 We could use the slice method. 42 00:02:54,369 --> 00:03:00,798 So let's say, first_three_items = grocery_list.slice. 43 00:03:00,798 --> 00:03:06,829 Now we give it the index, which is 0, and the number of items we want, which is 3. 44 00:03:07,880 --> 00:03:12,338 Now first_three_items contains "milk", "eggs", and "bread", 45 00:03:12,338 --> 00:03:16,720 and our grocery_list was unmodified by the slice command. 46 00:03:18,150 --> 00:03:20,419 Now we could give it any index we wanted. 47 00:03:20,419 --> 00:03:27,370 So we could say new_list is grocery_list.slice and then any index. 48 00:03:27,370 --> 00:03:29,300 Let’s give it the index of 2. 49 00:03:29,300 --> 00:03:35,460 And say we only want 2 items, and that's gonna be "bread" and "ice cream". 50 00:03:35,460 --> 00:03:37,870 Now that's what our new list contains as well. 51 00:03:38,960 --> 00:03:42,560 Try removing things from arrays now on your own, using Workspaces.