1 00:00:00,410 --> 00:00:03,000 So when we just left off we had created a shopping list and 2 00:00:03,000 --> 00:00:05,040 printed out the contents. 3 00:00:05,040 --> 00:00:09,920 Now let's go ahead and add on to this and add an item to this list. 4 00:00:11,150 --> 00:00:15,790 So let's go ahead and call add_list_item dot inspect, and 5 00:00:15,790 --> 00:00:20,624 what that's gonna do is the call the add_list_item method and 6 00:00:20,624 --> 00:00:24,890 then we're just gonna see what the output is first. 7 00:00:26,110 --> 00:00:30,804 So we click down into the console and type ruby shopping_list.rb. 8 00:00:30,804 --> 00:00:34,959 The list name is "Groceries", and we don't have any items, so let's go ahead and 9 00:00:34,959 --> 00:00:35,650 add an item. 10 00:00:35,650 --> 00:00:38,951 And the item is Milk. 11 00:00:38,951 --> 00:00:39,950 Uh-oh. 12 00:00:39,950 --> 00:00:44,690 Undefined local variable or method name for main object. 13 00:00:44,690 --> 00:00:46,050 That's because we spelled it wrong. 14 00:00:47,890 --> 00:00:51,070 Our program freaked out a little bit, and that's okay. 15 00:00:51,070 --> 00:00:53,850 It exited, but nothing catastrophic happened. 16 00:00:53,850 --> 00:00:56,660 So let's go ahead and clear the screen, and run that again. 17 00:00:56,660 --> 00:00:59,660 And actually, let's put a space here, format that just a little bit better. 18 00:01:01,130 --> 00:01:06,910 List name is "Groceries", and go ahead and add "Milk" to our grocery list. 19 00:01:06,910 --> 00:01:12,800 And we can see that we return a new hash with the key of "name", 20 00:01:12,800 --> 00:01:14,630 and the value of Milk. 21 00:01:15,660 --> 00:01:22,350 And that's okay, but what I think I would like to do is also get the quantity back. 22 00:01:22,350 --> 00:01:24,435 So let's go ahead and ask how much. 23 00:01:28,617 --> 00:01:32,300 And we'll get from standard input again, and 24 00:01:32,300 --> 00:01:36,744 then call to_i on that, which will turn it into a number. 25 00:01:40,264 --> 00:01:43,130 And now we'll put that into the hash. 26 00:01:44,850 --> 00:01:47,620 Now let's go ahead and run this again and see what happens. 27 00:01:48,730 --> 00:01:51,190 List name is "Groceries". 28 00:01:51,190 --> 00:01:52,170 Item is "Milk". 29 00:01:53,320 --> 00:01:54,430 And I only want one. 30 00:01:56,470 --> 00:02:01,340 Now we can see that this add_list_item method is returning a hash with two keys, 31 00:02:01,340 --> 00:02:02,790 "name" and "quantity". 32 00:02:04,930 --> 00:02:08,010 Now let's go ahead and take a look at our list and see how it looks. 33 00:02:10,670 --> 00:02:13,580 Gonna clear my screen here and run the program again. 34 00:02:19,020 --> 00:02:22,725 So here is the item that we're returning, but 35 00:02:22,725 --> 00:02:27,570 it's not being added to our list of items inside of the hash. 36 00:02:29,230 --> 00:02:30,940 So let's go ahead and fix that. 37 00:02:32,730 --> 00:02:38,530 What we're gonna do is use the push method and the reason is "items" is an array. 38 00:02:40,600 --> 00:02:44,790 So let's go ahead and type list.push, and 39 00:02:44,790 --> 00:02:50,690 then the argument that we're adding to push is all_list_item. 40 00:02:54,358 --> 00:02:57,410 Now, we can print out the contents of the list again. 41 00:03:01,012 --> 00:03:03,920 Let me clear my screen here and run it again. 42 00:03:05,780 --> 00:03:12,117 Okay, we've got our grocery list created, "Milk", and we only need one. 43 00:03:12,117 --> 00:03:16,680 Uh-oh, undefined method push for this hash. 44 00:03:17,930 --> 00:03:18,990 Now what could that be? 45 00:03:20,240 --> 00:03:24,660 Well, we're not pushing onto the list hash. 46 00:03:24,660 --> 00:03:29,810 We're pushing onto the list items inside of the hash. 47 00:03:31,900 --> 00:03:34,372 Now let's clear this and run it again. 48 00:03:38,511 --> 00:03:40,030 Okay. 49 00:03:40,030 --> 00:03:43,240 Now this is what our grocery list looks like. 50 00:03:43,240 --> 00:03:49,250 It's a hash with two keys, "name" which is "Groceries", 51 00:03:49,250 --> 00:03:55,003 "items", which is an array, which contains a hash. 52 00:03:55,003 --> 00:03:57,770 In our next video we're going to format this.