1 00:00:00,590 --> 00:00:02,696 Okay, so when we last left off, 2 00:00:02,696 --> 00:00:08,004 we have added onto our shopping list after calling the add_list_item method. 3 00:00:08,004 --> 00:00:12,800 And that put list items into an array of hashes. 4 00:00:14,310 --> 00:00:20,740 So now that we have all of this done, let's go ahead and print out our list. 5 00:00:20,740 --> 00:00:22,620 And we'll create a new method to do that. 6 00:00:24,280 --> 00:00:31,120 So what I'm gonna do is click up in here and create a method called print_list. 7 00:00:31,120 --> 00:00:36,490 And we'll pass in as an argument the list that we want to print. 8 00:00:37,640 --> 00:00:40,004 So we know that our list has a name. 9 00:00:42,671 --> 00:00:47,720 So let's go ahead and print that out as the first item here. 10 00:00:47,720 --> 00:00:51,570 Now we do that by using string interpolation. 11 00:00:51,570 --> 00:00:54,580 Now we know that the list name is a string. 12 00:00:54,580 --> 00:01:01,490 And we access the value of that by calling list['name']. 13 00:01:01,490 --> 00:01:05,530 Since it's a string, string interpolation will work just the same. 14 00:01:06,620 --> 00:01:08,660 It's just like calling any other variable. 15 00:01:10,110 --> 00:01:12,320 Now, let's go ahead and create a line. 16 00:01:14,100 --> 00:01:15,520 That'll just be four dashes. 17 00:01:16,560 --> 00:01:21,860 Now what we want to do is print out the items in the list. 18 00:01:21,860 --> 00:01:26,200 Now we know that the list items is an array containing hashes. 19 00:01:27,350 --> 00:01:31,337 What we can do is use a method called each. 20 00:01:31,337 --> 00:01:34,670 We'll get into this method a little bit more in an upcoming course, 21 00:01:34,670 --> 00:01:36,962 but for right now, we'll just follow along. 22 00:01:40,005 --> 00:01:42,360 So we call the method each. 23 00:01:42,360 --> 00:01:46,959 Now when we call the method each, we use this new piece of Ruby syntax called 24 00:01:46,959 --> 00:01:54,270 a block, and then pass in an argument. 25 00:01:54,270 --> 00:02:00,160 Blocks are written by typing either curly braces or the words do and end. 26 00:02:01,680 --> 00:02:09,222 So what this is going to do is take each item in this array of list items and 27 00:02:09,222 --> 00:02:13,129 assign it to a variable called item. 28 00:02:13,129 --> 00:02:18,250 And now, we can print out the item name and the item quantity. 29 00:02:20,600 --> 00:02:25,780 Now remember the list items array is an array of hashes. 30 00:02:25,780 --> 00:02:27,010 So let's go ahead and 31 00:02:27,010 --> 00:02:32,990 print out the item['name'] because we want to know what the name is of the item. 32 00:02:34,730 --> 00:02:41,520 And we'll also print out quantity and the item's quantity. 33 00:02:43,110 --> 00:02:45,120 And then we'll print out another separator. 34 00:02:47,500 --> 00:02:48,640 Okay. 35 00:02:48,640 --> 00:02:52,720 Now that we have this, let's go ahead and call print_list and 36 00:02:52,720 --> 00:02:54,380 send in our shopping list. 37 00:02:56,790 --> 00:02:58,920 Let's run this and see what happens. 38 00:02:58,920 --> 00:03:03,817 The list name is Groceries, that item that we're adding is Milk, and 39 00:03:03,817 --> 00:03:06,150 we're adding one thing of milk. 40 00:03:07,300 --> 00:03:11,204 Oh, it looks like we have an error on line 26. 41 00:03:13,090 --> 00:03:17,370 And the error is, no implicit conversion of fixnum into String. 42 00:03:19,180 --> 00:03:25,070 So what we have to do is tell Ruby that we want this number to be a string. 43 00:03:25,070 --> 00:03:27,795 And we use the to_s method to do that. 44 00:03:27,795 --> 00:03:29,360 Let me clear my screen here. 45 00:03:30,590 --> 00:03:32,990 And we'll run this again and see what happens. 46 00:03:34,890 --> 00:03:37,200 Now we're going to add Milk and one thing of Milk. 47 00:03:38,810 --> 00:03:41,320 And now here is our grocery list. 48 00:03:42,340 --> 00:03:44,180 Alright, that looks good, but 49 00:03:44,180 --> 00:03:46,870 we're going to clean this up a little bit more in the next video.