1 00:00:00,000 --> 00:00:04,662 [MUSIC] 2 00:00:04,662 --> 00:00:08,740 Hi, I'm Jason, and welcome to Build an Address Book with Ruby. 3 00:00:08,740 --> 00:00:11,620 We're going to be building a command line program that will let someone 4 00:00:11,620 --> 00:00:15,320 put in the names and information of all of their contacts. 5 00:00:15,320 --> 00:00:20,000 Then, we'll add in the ability to go through, and search these contacts. 6 00:00:20,000 --> 00:00:22,200 We're going to create our own classes for 7 00:00:22,200 --> 00:00:25,450 each different part of the address book program. 8 00:00:25,450 --> 00:00:30,590 This will encompass the address book itself, contacts, and their information. 9 00:00:30,590 --> 00:00:33,150 What we're doing is abstracting away the logic 10 00:00:33,150 --> 00:00:37,020 of these different parts of the program into their own classes. 11 00:00:37,020 --> 00:00:40,680 This is a good example of object oriented programming. 12 00:00:40,680 --> 00:00:44,300 The process of figuring out what logic goes to what class or 13 00:00:44,300 --> 00:00:46,580 object is called domain modeling. 14 00:00:46,580 --> 00:00:48,600 But that's not really super important to remember. 15 00:00:49,810 --> 00:00:53,350 Now, we have to start somewhere so we'll go ahead and start with the user or 16 00:00:53,350 --> 00:00:55,260 contact class. 17 00:00:55,260 --> 00:00:56,780 Go ahead and launch a workspace, and 18 00:00:56,780 --> 00:00:59,600 we'll begin to create the class which will hold our contact. 19 00:01:00,860 --> 00:01:03,080 Okay, so the first class that we're going to create for 20 00:01:03,080 --> 00:01:06,340 our address book is the contact class. 21 00:01:06,340 --> 00:01:13,220 The contact is going to encompass the idea of one of the people in your address book. 22 00:01:14,340 --> 00:01:16,990 So let's go ahead, I've launched a new Ruby workspace here. 23 00:01:16,990 --> 00:01:23,512 And go ahead and hit File, and New File, and we'll name this contact.rb. 24 00:01:23,512 --> 00:01:27,812 So here we are, and now we can go ahead and create our class. 25 00:01:32,212 --> 00:01:36,440 Now let's think about what the contact is going to have. 26 00:01:36,440 --> 00:01:41,630 The contact for our purposes will have a first name, middle name and last name. 27 00:01:43,080 --> 00:01:46,905 Now we're going to want to refer to those a little bit later, 28 00:01:46,905 --> 00:01:51,642 so for right now, let's go ahead and create attribute writers for those. 29 00:02:00,122 --> 00:02:04,540 And since we have the writers, let's go ahead and create methods to read them. 30 00:02:07,650 --> 00:02:11,042 So, the first_name method is going to return the first_name variable. 31 00:02:12,922 --> 00:02:18,628 The middle_name method is going to refer to the middle_name variable, 32 00:02:18,628 --> 00:02:24,250 and the last_name method is going to refer to the last_name variable. 33 00:02:27,430 --> 00:02:30,300 Now, at the bottom of this file, let's go ahead and just create a new contact, 34 00:02:30,300 --> 00:02:39,660 make sure everything is working okay, and we'll say json.first_name is Jason. 35 00:02:39,660 --> 00:02:40,770 I don't have a middle name. 36 00:02:42,380 --> 00:02:45,950 And my last name is Seifer. 37 00:02:48,060 --> 00:02:50,520 And let's go ahead and just print out that contact. 38 00:02:50,520 --> 00:02:53,670 Now I'm going to click down into the bottom console here, and 39 00:02:53,670 --> 00:02:56,264 type ruby contact.rb. 40 00:02:57,740 --> 00:03:00,110 Okay, we have a contact. 41 00:03:01,740 --> 00:03:08,260 And let me just go ahead and put the first_name, space, and a last_name. 42 00:03:10,370 --> 00:03:12,830 And just make sure that this is working correctly. 43 00:03:12,830 --> 00:03:13,610 Okay, that looks good. 44 00:03:13,610 --> 00:03:15,870 Now when we think about our address book, 45 00:03:15,870 --> 00:03:21,320 we may want to refer to a contact in different ways. 46 00:03:21,320 --> 00:03:25,550 Maybe we want to print out their full name, like we did right here. 47 00:03:25,550 --> 00:03:29,748 So let's go ahead and create a full_name method, 48 00:03:29,748 --> 00:03:33,562 that will print out the contact's full name. 49 00:03:33,562 --> 00:03:37,219 And instead of actually printing it out, let's just go ahead and 50 00:03:37,219 --> 00:03:39,690 return a string of the full name. 51 00:03:39,690 --> 00:03:43,821 Now if we were just returning this, 52 00:03:43,821 --> 00:03:50,856 we could do first_name, middle_name, and last_name. 53 00:03:54,116 --> 00:03:57,230 Now watch what happens if we print this out. 54 00:03:57,230 --> 00:04:01,930 We'll just print Jason.full_name. 55 00:04:01,930 --> 00:04:03,680 Now notice I don't have a middle initial. 56 00:04:06,000 --> 00:04:09,380 So when we print this out, there's a little space right here. 57 00:04:10,410 --> 00:04:11,390 So let's go ahead and fix that. 58 00:04:13,430 --> 00:04:18,900 And instead of doing this, let's go ahead and assemble this full name in parts. 59 00:04:20,320 --> 00:04:24,295 So we'll say the full_name is equal to the first_name. 60 00:04:25,700 --> 00:04:30,100 Now right here full_name is just a local variable inside the full_name method. 61 00:04:30,100 --> 00:04:36,342 It's okay to name variables the same thing as methods when you are inside a method. 62 00:04:36,342 --> 00:04:38,160 You're not gonna have any conflict. 63 00:04:38,160 --> 00:04:43,317 So we've got full_name and then we can say, 64 00:04:43,317 --> 00:04:46,582 if the middle_name is nil. 65 00:04:51,402 --> 00:04:57,482 If the middle name is not nil, what we'll do is add a space to this full name. 66 00:05:03,542 --> 00:05:10,070 And then add the middle_name method which returns the variable to that full_name. 67 00:05:11,170 --> 00:05:13,522 And now we can just add the last_name onto it. 68 00:05:20,262 --> 00:05:21,990 And then we just return this variable. 69 00:05:23,770 --> 00:05:27,372 And we do an implicit return as the last line of the method. 70 00:05:30,594 --> 00:05:33,660 Then when we run this, we can see that works correctly. 71 00:05:36,180 --> 00:05:42,342 And let's go ahead and try one with a middle_name. 72 00:05:56,942 --> 00:05:58,390 All right. 73 00:05:58,390 --> 00:05:59,280 And we run that again. 74 00:06:00,900 --> 00:06:02,140 And this works correctly. 75 00:06:02,140 --> 00:06:05,580 And that is a pretty good start to our contact class.