1 00:00:00,750 --> 00:00:04,210 Okay, so we have address book looking pretty good. 2 00:00:04,210 --> 00:00:08,890 We can find people by name and we can find people by phone number. 3 00:00:08,890 --> 00:00:14,730 Finally, let's go ahead and add the ability to find somebody by their address. 4 00:00:14,730 --> 00:00:19,220 Now, this is going to follow a similar pattern as finding by name and 5 00:00:19,220 --> 00:00:23,580 phone number, except this time we're going to be querying by the address. 6 00:00:23,580 --> 00:00:28,721 So, let's go ahead and create a method to find by an address. 7 00:00:33,010 --> 00:00:34,261 Now if we scroll up, 8 00:00:34,261 --> 00:00:38,455 we can see we have this pattern of creating an empty results array and 9 00:00:38,455 --> 00:00:43,754 then manipulating the search to get sent in, and then looping through the contacts, 10 00:00:43,754 --> 00:00:48,800 and appending to the array, unless the contact is included in that array. 11 00:00:48,800 --> 00:00:53,040 So let's go ahead and follow that same pattern now, and this time, 12 00:00:53,040 --> 00:00:58,060 what we're going to send in is a query, because we're going to be looping and 13 00:00:58,060 --> 00:01:00,240 finding the address, but we'll see that in just a second. 14 00:01:02,100 --> 00:01:06,860 So, we'll set up our empty results array, and then we'll create our search variable, 15 00:01:10,850 --> 00:01:14,430 which is just going to be a lowercase version of the query. 16 00:01:15,500 --> 00:01:17,494 Now we can loop through our contacts. 17 00:01:22,369 --> 00:01:26,440 And then we're going to have to loop through our addresses too. 18 00:01:26,440 --> 00:01:29,860 Now let's go ahead and look at the address class for a moment. 19 00:01:29,860 --> 00:01:34,370 We have this to_s method, and we learned before that we can use the include 20 00:01:34,370 --> 00:01:39,260 method to see if one string is included in another. 21 00:01:39,260 --> 00:01:43,580 So we're gonna use the long form of the address to see whether or 22 00:01:43,580 --> 00:01:46,880 not the search is included in there. 23 00:01:46,880 --> 00:01:51,140 But we need to loop through each address for 24 00:01:51,140 --> 00:01:56,050 each contact to see if the query is contained there. 25 00:01:56,050 --> 00:01:57,846 So let's go ahead and do that. 26 00:01:57,846 --> 00:02:03,487 We're in a contact right now, so we'll go ahead and loop through the addresses. 27 00:02:08,218 --> 00:02:14,502 Now we can say if the long form of the address downcase that, 28 00:02:14,502 --> 00:02:19,670 and then we'll see if that includes the search. 29 00:02:21,550 --> 00:02:24,085 Then we can follow the same pattern as before. 30 00:02:26,999 --> 00:02:30,513 And append the contact to the results, 31 00:02:30,513 --> 00:02:34,780 unless the results already have that contact. 32 00:02:35,980 --> 00:02:38,070 So, we don't have any duplicates when we print it out. 33 00:02:39,980 --> 00:02:44,704 And then finally, we can use that print results method to display the results. 34 00:02:54,706 --> 00:02:58,375 So now, all we have to do is, we can scroll down here. 35 00:03:04,338 --> 00:03:10,915 And let's just search for two, since we know that Nick has an address on Two Lane. 36 00:03:14,016 --> 00:03:17,088 And comment those out just to clean up the output a little bit. 37 00:03:19,084 --> 00:03:21,560 And then go down in the console here. 38 00:03:21,560 --> 00:03:26,490 If I type ruby address_book.rb, 39 00:03:26,490 --> 00:03:31,640 we can see that we have Nick turning up in the address search results for 40 00:03:31,640 --> 00:03:34,590 two, which is exactly what we wanted.