1 00:00:00,025 --> 00:00:04,843 [MUSIC] 2 00:00:04,843 --> 00:00:08,875 We have all of our contacts successfully in the program but just printing out 3 00:00:08,875 --> 00:00:13,970 a list doesn't do us much good if we don't have the ability to search through. 4 00:00:13,970 --> 00:00:17,540 We're going to use some methods that take blocks to search through our different 5 00:00:17,540 --> 00:00:18,940 contacts. 6 00:00:18,940 --> 00:00:22,450 Let's go ahead and try adding that ability now using work spaces. 7 00:00:23,540 --> 00:00:24,070 All right. 8 00:00:24,070 --> 00:00:30,130 So, first up let's go ahead and add the ability to find a contact by name. 9 00:00:30,130 --> 00:00:32,320 We can print out the contact list. 10 00:00:32,320 --> 00:00:34,940 But, we want to be able to search through it. 11 00:00:37,430 --> 00:00:39,870 So let's go ahead and write a method. 12 00:00:40,920 --> 00:00:42,640 Called find_by_name. 13 00:00:45,447 --> 00:00:47,350 And we'll pass in a name. 14 00:00:49,530 --> 00:00:52,170 Now, let's think about how we're gonna do this. 15 00:00:52,170 --> 00:00:58,180 What we can do is iterate over each contact in the contact list. 16 00:00:58,180 --> 00:01:03,810 And then, if the name matches the argument to this method we'll go ahead and 17 00:01:03,810 --> 00:01:05,840 return that contact. 18 00:01:05,840 --> 00:01:07,660 So how are we gonna do that? 19 00:01:07,660 --> 00:01:11,670 Well, we can start by having an empty array 20 00:01:11,670 --> 00:01:15,430 of search results which will be the contacts in our contacts list. 21 00:01:19,270 --> 00:01:26,200 And then we'll say the search is the name which we send into the method. 22 00:01:26,200 --> 00:01:28,280 Same thing here and here. 23 00:01:29,820 --> 00:01:32,330 But now let's think about this for just a moment. 24 00:01:32,330 --> 00:01:37,550 If somebody's searching for part of a name or they've capitalized the name, 25 00:01:37,550 --> 00:01:40,610 we still want the search to return the correct thing. 26 00:01:40,610 --> 00:01:46,930 So what we’re going to do is make the search query lowercase. 27 00:01:46,930 --> 00:01:51,560 And then when we’re looking through the different contacts we’ll also match that 28 00:01:51,560 --> 00:01:56,760 against the lowercase version of the first, middle or last name. 29 00:01:56,760 --> 00:02:00,280 So let's go ahead and iterate through the contacts. 30 00:02:00,280 --> 00:02:03,640 And then if it matches, we'll append that contact to the results array. 31 00:02:11,901 --> 00:02:15,193 So, here's a loop, and now we'll say, 32 00:02:15,193 --> 00:02:20,378 contact.first_name, and remember, we'll downcase this. 33 00:02:20,378 --> 00:02:24,365 So now we're dealing with a lower case version of the first name, and 34 00:02:24,365 --> 00:02:26,635 strings have a method called include, 35 00:02:26,635 --> 00:02:29,890 which lets you see if a string includes another string. 36 00:02:32,530 --> 00:02:35,900 So we can say if this includes our search, 37 00:02:38,240 --> 00:02:42,370 we can append this contact to the result array. 38 00:02:44,200 --> 00:02:48,170 Now, what we can do is go through and print out the results. 39 00:02:52,871 --> 00:02:57,240 And then, just to be a little bit more clear, we'll put in the search term. 40 00:02:59,070 --> 00:03:01,190 Now we can iterate over the results. 41 00:03:04,140 --> 00:03:06,740 Remember, this is going to be a contact. 42 00:03:09,580 --> 00:03:11,440 Now we can print out the contact's name. 43 00:03:11,440 --> 00:03:16,350 We'll print out their full name. 44 00:03:19,660 --> 00:03:24,860 And we'll go ahead and print their phone numbers, and 45 00:03:24,860 --> 00:03:28,790 we'll go ahead and print the addresses, and we'll also print a new line. 46 00:03:31,230 --> 00:03:33,365 So now let's go down here, and 47 00:03:33,365 --> 00:03:38,058 instead of printing out the contact list, we can comment that out. 48 00:03:43,306 --> 00:03:47,030 And let's first try searching for Nick in all lower case and 49 00:03:47,030 --> 00:03:52,260 when we run this we should hopefully see Nick's contact information printed out. 50 00:03:56,897 --> 00:04:00,670 Oh, undefined local variable or method, seach. 51 00:04:00,670 --> 00:04:02,323 That would appear to be a typo. 52 00:04:06,229 --> 00:04:06,970 Here we go. 53 00:04:08,120 --> 00:04:13,080 That's on line 18, that all looks good. 54 00:04:13,080 --> 00:04:14,068 Let's run this again. 55 00:04:16,391 --> 00:04:20,439 Okay, this looks good, that's what we wanted. 56 00:04:22,710 --> 00:04:26,790 Now, let's go ahead and change this to an n, since both Nick and 57 00:04:26,790 --> 00:04:29,460 I have that letter in our names. 58 00:04:29,460 --> 00:04:32,430 And then we should hopefully see Jason and Nick here. 59 00:04:33,690 --> 00:04:35,258 And we do. That looks good. 60 00:04:39,397 --> 00:04:43,280 Let's go ahead and change this to search for the letter e. 61 00:04:47,260 --> 00:04:49,720 And that returned no search results. 62 00:04:51,240 --> 00:04:55,030 Even though both of our last names include the letter e. 63 00:05:00,208 --> 00:05:02,200 So let's go ahead and add an or statement. 64 00:05:03,740 --> 00:05:07,472 And we'll say if contact.last_name. 65 00:05:11,696 --> 00:05:16,067 Includes the search, we can push that to the search results. 66 00:05:21,123 --> 00:05:22,340 Let's go ahead and run this again. 67 00:05:23,340 --> 00:05:27,715 And, hey, we get the exact same results that we were expecting. 68 00:05:30,335 --> 00:05:36,775 And actually we could include the full name here, instead of searching for 69 00:05:36,775 --> 00:05:39,155 the first and last names, we can get the middle name in there, as well. 70 00:05:42,035 --> 00:05:43,737 Run that one more time. 71 00:05:43,737 --> 00:05:45,965 All right, now we have a name search working.