1 00:00:00,290 --> 00:00:02,420 Now that our contact class is all set up, 2 00:00:02,420 --> 00:00:05,560 we can start working on the other parts of the program. 3 00:00:05,560 --> 00:00:07,710 Since they're not quite addressed by built in classes, 4 00:00:07,710 --> 00:00:09,630 we're going to have to write our own. 5 00:00:09,630 --> 00:00:10,210 Get it? 6 00:00:10,210 --> 00:00:11,250 Addressed? 7 00:00:11,250 --> 00:00:12,780 It's a bad pun about addresses. 8 00:00:13,790 --> 00:00:16,550 Anyway, we'll be writing our own address class now 9 00:00:16,550 --> 00:00:19,780 in addition to another supporting class for our program. 10 00:00:19,780 --> 00:00:22,500 Let's get to it using workspaces. 11 00:00:22,500 --> 00:00:24,300 So our contacts have phone numbers. 12 00:00:24,300 --> 00:00:28,470 Now let's go ahead and have addresses for our contacts as well. 13 00:00:29,670 --> 00:00:35,418 Now we can do that by creating another class for an address. 14 00:00:35,418 --> 00:00:40,136 So we'll go ahead and create a new file, you can do that by right clicking and 15 00:00:40,136 --> 00:00:44,150 selecting new file on the side bar, or go file and new file. 16 00:00:44,150 --> 00:00:47,550 And we will call this address.rb. 17 00:00:50,152 --> 00:00:52,390 So now we can create our address class. 18 00:00:53,790 --> 00:00:56,630 And addresses are gonna be similar to phone numbers. 19 00:00:56,630 --> 00:01:01,300 They can have a street with potentially two lines and then a city, state, and 20 00:01:01,300 --> 00:01:02,400 postal code. 21 00:01:02,400 --> 00:01:05,180 Now, we also wanna know what kind of address it is. 22 00:01:05,180 --> 00:01:09,630 So, we'll put that in there as an attribute as well. 23 00:01:11,600 --> 00:01:16,860 So we'll have the kind, the first line of the street address, 24 00:01:16,860 --> 00:01:22,780 second line of the street address, the city, state, and postal code. 25 00:01:23,790 --> 00:01:28,310 Now just like with phone numbers, let's go ahead and override the two S method. 26 00:01:29,440 --> 00:01:34,700 Except there are two different ways that we may wanna be formatting addresses. 27 00:01:34,700 --> 00:01:38,530 So we can use that pattern that we used earlier with contacts, and 28 00:01:38,530 --> 00:01:43,210 we'll say the format that we want by default is going to be short. 29 00:01:43,210 --> 00:01:45,431 So now we can look at the format. 30 00:01:47,523 --> 00:01:52,451 And let's go ahead and create a little string here, 31 00:01:52,451 --> 00:01:57,730 to hold our address, which we will implicitly return. 32 00:02:00,960 --> 00:02:08,940 Now when we're using the short format, we will add on the kind of address, 33 00:02:10,530 --> 00:02:16,250 so we will say home, colon and then the street address all on one line. 34 00:02:20,010 --> 00:02:22,380 That will be the first line of the street address. 35 00:02:23,850 --> 00:02:27,397 And then we can say, 36 00:02:27,397 --> 00:02:32,000 if there is a second line. 37 00:02:32,000 --> 00:02:35,620 We'll add a space to this string because remember it's all on one line. 38 00:02:37,300 --> 00:02:42,680 And then the street two attribute and 39 00:02:42,680 --> 00:02:47,150 then we can return the comma and 40 00:02:47,150 --> 00:02:52,480 the city, state and postal code. 41 00:02:54,670 --> 00:02:57,932 And let's just go ahead and print that out real quick, and make sure it works. 42 00:03:17,031 --> 00:03:18,889 This is not my real home address. 43 00:03:22,876 --> 00:03:26,010 And let's just go ahead and print that out using the short format. 44 00:03:27,710 --> 00:03:30,370 So now we'll run this very quickly and make sure it works. 45 00:03:30,370 --> 00:03:33,550 And type ruby address.rb. 46 00:03:33,550 --> 00:03:39,210 Okay, that looks good, and let's go ahead and create the long format as well. 47 00:03:39,210 --> 00:03:43,990 So when the argument that we send in to this to_s method is long, 48 00:03:45,800 --> 00:03:52,015 we will say it is the street_1 variable plus a new line. 49 00:03:52,015 --> 00:03:56,253 [NOISE] And then we can also say, 50 00:03:56,253 --> 00:04:00,825 here is the street two variable. 51 00:04:00,825 --> 00:04:01,850 Plus a new line. 52 00:04:03,010 --> 00:04:06,840 And remember we can put if statements all on the same line if we want to. 53 00:04:10,100 --> 00:04:18,609 So we'll just say we'll add on to the address variable the street two variable, 54 00:04:18,609 --> 00:04:22,986 and a new line if street two is not nil, and 55 00:04:22,986 --> 00:04:29,393 then we could also add on the city, state and postal_code. 56 00:04:35,621 --> 00:04:37,839 Now let's just go ahead and print a new line here. 57 00:04:42,020 --> 00:04:44,538 And print it out in the long format as well. 58 00:04:48,017 --> 00:04:49,120 Okay, that looks good. 59 00:04:49,120 --> 00:04:55,510 We've got our home address printed in the short format and in the long format. 60 00:04:56,550 --> 00:05:00,420 In our next video, we'll add this back to the contact class.