1 00:00:00,660 --> 00:00:04,680 Now that we know how to create an array, let's look at how to add things to it. 2 00:00:05,720 --> 00:00:07,060 Once we have an array, 3 00:00:07,060 --> 00:00:10,950 there are a few different ways of adding items to that array. 4 00:00:10,950 --> 00:00:13,570 Let's see how that works now using workspaces. 5 00:00:14,660 --> 00:00:20,620 So here's another workspace, and I've created a file called arr_addition.rb. 6 00:00:20,620 --> 00:00:23,760 If you want to check out what we did in IRB in the last video, 7 00:00:23,760 --> 00:00:27,740 it'll be there in array_creation.rb. 8 00:00:27,740 --> 00:00:30,270 So now let's go ahead and check out this file. 9 00:00:31,370 --> 00:00:33,610 Here we have our grocery_list array. 10 00:00:34,710 --> 00:00:38,830 It contains three items, "milk", "eggs" and "bread". 11 00:00:40,120 --> 00:00:44,830 We can see the contents of an array if we run it using a Ruby program file by typing 12 00:00:44,830 --> 00:00:50,975 puts, and we can do grocery_list.inspect. 13 00:00:50,975 --> 00:00:56,040 And that will print out the contents of the array onto the screen. 14 00:00:56,040 --> 00:01:01,524 Let's go ahead and run this file by typing ruby array_addition.rb. 15 00:01:02,970 --> 00:01:05,310 Now we can see we have our array. 16 00:01:06,550 --> 00:01:09,910 Let's go ahead and add something on to this. 17 00:01:09,910 --> 00:01:16,890 So I've just clicked up onto line two and if I want to add on to the grocery list, 18 00:01:16,890 --> 00:01:22,380 I could say grocery_list and then two less than signs and add another item. 19 00:01:24,400 --> 00:01:28,580 And that will append the item onto the end of the array. 20 00:01:30,040 --> 00:01:31,810 Now if I run this, 21 00:01:31,810 --> 00:01:35,560 we can see that there is another item at the end of the array called "carrots". 22 00:01:37,150 --> 00:01:40,120 Another method of adding something to the end of an array 23 00:01:42,800 --> 00:01:45,720 is to use a method called push. 24 00:01:45,720 --> 00:01:50,230 So we could say grocery_list and then a dot and then push and 25 00:01:50,230 --> 00:01:54,210 then open parenthesis and the word "potatoes". 26 00:01:54,210 --> 00:01:57,420 This is getting to be a pretty big grocery list. 27 00:01:58,950 --> 00:02:01,890 Now if we print this out, we can see we have "carrots" and 28 00:02:01,890 --> 00:02:04,590 "potatoes" at the end of our original array. 29 00:02:06,230 --> 00:02:10,950 But what happens if we wanted to add something to the beginning of the array? 30 00:02:10,950 --> 00:02:15,337 In that case, we could use the method called unshift. 31 00:02:17,837 --> 00:02:21,062 Which, instead of adding something to the end of the array, 32 00:02:21,062 --> 00:02:22,780 we'll add it to the beginning. 33 00:02:24,860 --> 00:02:27,900 And we'll put "celery" at the beginning of the grocery list 34 00:02:27,900 --> 00:02:29,090 because we're eating healthy. 35 00:02:30,630 --> 00:02:35,150 Now if we run this, we can see that "celery" is here at the beginning 36 00:02:35,150 --> 00:02:37,470 as opposed to "carrots" and "potatoes" 37 00:02:37,470 --> 00:02:38,280 which were at the end. 38 00:02:40,510 --> 00:02:44,690 If we wanted to we could also use 39 00:02:44,690 --> 00:02:49,380 the plus equals operator and add more items onto the array that way. 40 00:02:50,450 --> 00:02:52,050 Let's go ahead and get some desserts in here. 41 00:02:53,930 --> 00:02:56,320 So we'll add "ice cream" and "pie". 42 00:02:57,700 --> 00:03:00,220 And you'll notice that I'm adding another array here. 43 00:03:01,840 --> 00:03:04,080 Let's run that and see what happens. 44 00:03:04,080 --> 00:03:05,610 and it looks like "ice cream" and 45 00:03:05,610 --> 00:03:09,140 "pie" were successfully added to the end of our array. 46 00:03:10,930 --> 00:03:12,630 Now, we're not limited to just Strings. 47 00:03:14,420 --> 00:03:16,460 We could also add numbers if we wanted as well. 48 00:03:17,960 --> 00:03:21,590 Or actually, almost any Ruby type can go in there. 49 00:03:22,610 --> 00:03:26,000 Try adding on to your arrays now using WorkSpaces.