1 00:00:00,000 --> 00:00:04,662 [MUSIC] 2 00:00:04,662 --> 00:00:06,300 Hi, I'm Jason. 3 00:00:06,300 --> 00:00:09,450 Let's start talking about arrays in Ruby. 4 00:00:09,450 --> 00:00:14,530 So far, some basic types that we've learned about are strings and numbers. 5 00:00:14,530 --> 00:00:18,810 Strings let us work with textual data in our programs, and 6 00:00:18,810 --> 00:00:20,420 numbers with numerical data. 7 00:00:21,630 --> 00:00:24,280 Let's say that we wanted to create a shopping list. 8 00:00:24,280 --> 00:00:25,860 Inside of the shopping list, 9 00:00:25,860 --> 00:00:29,470 our grocery items could be something like milk, eggs, and bread. 10 00:00:30,840 --> 00:00:34,340 We could store that list in a string separated by commas. 11 00:00:34,340 --> 00:00:38,680 Or we could use three different strings called something like item one, 12 00:00:38,680 --> 00:00:40,800 item two, item three, and so on. 13 00:00:41,860 --> 00:00:46,980 However, Ruby gives us a type specifically to work with that kind of data. 14 00:00:46,980 --> 00:00:48,240 We could use an array. 15 00:00:49,520 --> 00:00:52,190 An array is a container of data. 16 00:00:52,190 --> 00:00:54,500 You can think of it as a list. 17 00:00:54,500 --> 00:00:59,470 We could store all of our different grocery items inside of the array. 18 00:00:59,470 --> 00:01:04,510 Arrays can store any type of thing that we have access to in Ruby. 19 00:01:04,510 --> 00:01:07,290 Let's see how to make an array now using WorkSpaces. 20 00:01:08,760 --> 00:01:14,060 So I have gone in and created a new Ruby workspace. 21 00:01:14,060 --> 00:01:17,360 Now, over here in the console, going to drag this up. 22 00:01:19,150 --> 00:01:24,780 And we're going to launch IRB by typing irb and press Enter. 23 00:01:25,930 --> 00:01:30,130 Now let's go ahead and create our very first array. 24 00:01:30,130 --> 00:01:33,820 We'll do that by typing the name of the variable, 25 00:01:33,820 --> 00:01:38,590 in this case we'll say we wanna create a grocery list. 26 00:01:38,590 --> 00:01:43,460 Now I'm going to type a space and the equals sign, another space, and 27 00:01:43,460 --> 00:01:50,180 then capital a, y, a dot, and the word new. 28 00:01:51,550 --> 00:01:57,302 This will create a new array, initialized to the variable called grocery list. 29 00:01:57,302 --> 00:02:01,146 Now, right below, you'll see we have an equal sign, and 30 00:02:01,146 --> 00:02:05,081 a greater than sign, which is going to be the return value. 31 00:02:05,081 --> 00:02:09,922 And the return value is an open bracket, and then a closed bracket. 32 00:02:09,922 --> 00:02:13,544 That's Ruby's sign for an array. 33 00:02:13,544 --> 00:02:18,560 Now we can take a look at this grocery list item and it returns the same thing. 34 00:02:19,890 --> 00:02:21,710 There’s another way that we can create an array. 35 00:02:24,140 --> 00:02:26,820 And that is to use the brackets themselves. 36 00:02:26,820 --> 00:02:30,870 So, we can type an open square bracket and then a closed square bracket and 37 00:02:30,870 --> 00:02:32,690 we get the same thing. 38 00:02:32,690 --> 00:02:34,330 Now this is useful, but 39 00:02:34,330 --> 00:02:39,030 it’s not as useful as having a grocery list with items inside of it. 40 00:02:40,380 --> 00:02:45,142 Lets go ahead and create a grocery list array with our items already in it. 41 00:02:48,921 --> 00:02:50,460 So we'll put three items in here. 42 00:02:51,590 --> 00:02:54,860 The way we add items to an array when creating it, 43 00:02:54,860 --> 00:03:00,270 is to put the items inside of the brackets, separated by commas. 44 00:03:00,270 --> 00:03:05,010 So right here, I've opened the bracket and we'll put the string milk, and 45 00:03:05,010 --> 00:03:09,530 then we'll also add eggs and bread into this array. 46 00:03:09,530 --> 00:03:11,940 Now we separate the items by a comma. 47 00:03:13,200 --> 00:03:16,100 So in this case, we have milk as an item, 48 00:03:16,100 --> 00:03:20,790 and then a comma, eggs, another comma, and bread. 49 00:03:20,790 --> 00:03:25,322 We don't put a closing comma before the closing square bracket. 50 00:03:27,382 --> 00:03:31,903 Now when we look at the grocery list, it has the same thing as an empty array, 51 00:03:31,903 --> 00:03:33,680 except the items are inside. 52 00:03:35,000 --> 00:03:37,020 Now we're not limited to just strings. 53 00:03:38,400 --> 00:03:40,080 We could add numbers as well. 54 00:03:41,700 --> 00:03:44,530 And you'll notice the numbers don't have quotes around them, 55 00:03:44,530 --> 00:03:48,260 just like when we were working with strings and numbers by themselves. 56 00:03:50,380 --> 00:03:55,040 Now another way to create an array, if we wanted to create an array of just strings, 57 00:03:57,430 --> 00:04:01,850 we could use the %w syntax. 58 00:04:01,850 --> 00:04:06,331 And then we don't have to surround our strings with quotes. 59 00:04:09,654 --> 00:04:14,660 Now if we look at our grocery list, it has the same thing as if we did 60 00:04:14,660 --> 00:04:19,594 surround it with quotes, and use the square bracket notation. 61 00:04:21,814 --> 00:04:24,740 Another thing that we can do is use an uppercase W. 62 00:04:26,540 --> 00:04:29,800 Now just like when we were printing things out with strings and 63 00:04:29,800 --> 00:04:34,930 having them interpolated, using a capital W will allow us to do interpolation. 64 00:04:38,320 --> 00:04:43,040 So I have assigned a variable called item equal to a string called milk. 65 00:04:43,040 --> 00:04:47,660 Now, when I create this grocery list array, using a capital W, 66 00:04:47,660 --> 00:04:49,630 item will be interpolated. 67 00:04:49,630 --> 00:04:54,810 And the array comes back with a string of milk, another string of eggs, and 68 00:04:54,810 --> 00:04:56,110 another string of bread. 69 00:04:58,840 --> 00:05:02,350 Try both methods of creating arrays now, using WorkSpaces.