1 00:00:00,170 --> 00:00:04,310 Okay, so, we have our contact class all set up in here. 2 00:00:04,310 --> 00:00:06,110 And if we think about that, that's great. 3 00:00:06,110 --> 00:00:07,960 We can have a list of contacts, 4 00:00:07,960 --> 00:00:13,020 but wouldn't we also want to enter a contact's phone number? 5 00:00:13,020 --> 00:00:13,740 Yes we would. 6 00:00:14,810 --> 00:00:19,540 So what we're going to do is create a class to store phone numbers. 7 00:00:19,540 --> 00:00:21,809 So hit File > New. 8 00:00:21,809 --> 00:00:25,840 And we'll call this class phone_number. 9 00:00:25,840 --> 00:00:28,749 And we create a phone_number.rb. 10 00:00:28,749 --> 00:00:32,060 Now we can create our PhoneNumber class. 11 00:00:32,060 --> 00:00:36,650 And this is gonna be a really simple class. 12 00:00:36,650 --> 00:00:40,460 We're not really gonna be working with phone numbers too much by themselves. 13 00:00:40,460 --> 00:00:44,090 So, we're just gonna make two attribute accessors here. 14 00:00:44,090 --> 00:00:47,870 The kind of phone number it is like home, work, cell. 15 00:00:47,870 --> 00:00:51,070 And, we're just going to have another attribute called number which 16 00:00:51,070 --> 00:00:52,850 stores the number. 17 00:00:52,850 --> 00:00:55,880 So, we can use an attribute accessor for that. 18 00:00:55,880 --> 00:00:58,450 We'll do one for the kind and the number. 19 00:00:59,530 --> 00:01:02,139 And then since we have that, 20 00:01:02,139 --> 00:01:07,050 we can go ahead and define our to_s method here. 21 00:01:07,050 --> 00:01:12,096 And let's just say it's the "#{kind}: #{number}". 22 00:01:12,096 --> 00:01:17,090 Now, this is a pretty simple class so we're not gonna make sure it works. 23 00:01:17,090 --> 00:01:23,730 It's very simple so let's go ahead and now we can add phone numbers to our contacts. 24 00:01:25,040 --> 00:01:34,130 So let's go ahead and say our contact has an attribute reader for 25 00:01:34,130 --> 00:01:38,299 phone numbers because a contact will be able to have more than one phone number. 26 00:01:39,380 --> 00:01:46,690 But, this means that when we create our contact class and initialize it, 27 00:01:48,640 --> 00:01:53,280 we'll set the phone numbers to be an empty array. 28 00:01:55,140 --> 00:02:00,800 Now that we've done that, let's go ahead and create a method to add a phone number. 29 00:02:00,800 --> 00:02:02,179 To our contact. 30 00:02:08,518 --> 00:02:12,022 And we'll make this method take two arguments, 31 00:02:12,022 --> 00:02:16,465 the kind and the number which will correspond to the different 32 00:02:16,465 --> 00:02:20,322 attributes that we have In the phone_number class. 33 00:02:22,277 --> 00:02:26,398 So once we do that, we'll just create a new phone_number instance. 34 00:02:29,518 --> 00:02:33,438 And we'll say the phone_number.kind is the kind argument. 35 00:02:35,118 --> 00:02:37,610 And the number is the number argument. 36 00:02:38,870 --> 00:02:42,610 Once we have that we wanna append it to the phone numbers array. 37 00:02:44,980 --> 00:02:54,101 So we can say phone_numbers.push(phone_number). 38 00:02:54,101 --> 00:02:58,301 So let's go ahead and save that and makes sure it works. 39 00:03:03,180 --> 00:03:06,189 I am going to take out some of these print statements because we don't need 40 00:03:06,189 --> 00:03:07,120 to test that anymore. 41 00:03:10,041 --> 00:03:13,810 So we're printing out the Jason contact. 42 00:03:15,060 --> 00:03:21,558 And we'll say jason.add_phone_number("Home",123-456-78- 43 00:03:21,558 --> 00:03:29,710 90") that is my real phone number, feel free to call me. 44 00:03:31,930 --> 00:03:37,300 And let's go ahead and add another one for work 45 00:03:40,000 --> 00:03:46,980 and I'll add my real work phone number here, okay. 46 00:03:46,980 --> 00:03:53,540 Now let's go ahead and just inspect the Jason contact and see what it looks like. 47 00:03:54,630 --> 00:03:58,119 So I'm gonna run ruby contact.rb. 48 00:03:58,119 --> 00:03:59,660 And you'll see we get this method. 49 00:03:59,660 --> 00:04:03,100 Uninitialized constant Contact PhoneNumber. 50 00:04:04,160 --> 00:04:05,120 Now why did we get that? 51 00:04:06,260 --> 00:04:11,750 Well it's because our phone number class is in a separate file. 52 00:04:11,750 --> 00:04:15,630 So when we try and access a class that's in another file, 53 00:04:15,630 --> 00:04:19,290 we need to tell Ruby that that exists someplace else. 54 00:04:19,290 --> 00:04:21,340 Or else Ruby doesn't know where it is. 55 00:04:21,340 --> 00:04:23,590 We do that using the require key word. 56 00:04:28,110 --> 00:04:29,730 And then we give it the path to the file. 57 00:04:30,900 --> 00:04:36,220 So we're requiring, in the same directory as this the phone number class. 58 00:04:37,440 --> 00:04:39,680 I'm gonna clear my screen and run this again. 59 00:04:41,510 --> 00:04:46,550 And you'll notice, now when we do the inspect here, 60 00:04:46,550 --> 00:04:51,030 we've got this contact with this phone numbers 61 00:04:51,030 --> 00:04:55,960 array which contains two different phone numbers. 62 00:04:57,640 --> 00:04:59,970 And this is exactly what we want. 63 00:04:59,970 --> 00:05:01,530 But now, let's just go ahead and 64 00:05:01,530 --> 00:05:04,800 create a method to print out the phone numbers while we are at it. 65 00:05:04,800 --> 00:05:07,540 So, that we don't have to inspect it each time. 66 00:05:10,370 --> 00:05:13,400 Then we'll just call this method, print phone numbers. 67 00:05:16,300 --> 00:05:19,220 And this is going to be really easy, since phone numbers is an array, 68 00:05:19,220 --> 00:05:21,500 we can just use the each method. 69 00:05:21,500 --> 00:05:26,115 And I'll say Phone Numbers right here and then, we can just loop through them 70 00:05:26,115 --> 00:05:29,749 using the each method and print out all of the phone numbers. 71 00:05:34,030 --> 00:05:37,809 Now remember, each will take a block with one argument. 72 00:05:45,769 --> 00:05:52,828 And now we can call jason.print_phone_numbers. 73 00:05:56,049 --> 00:05:56,970 Run that again. 74 00:05:58,110 --> 00:05:58,680 And here we go. 75 00:05:58,680 --> 00:06:03,120 Now, you might be wondering why this knows 76 00:06:03,120 --> 00:06:07,590 how to print out the kind of the phone number, and then the number, itself. 77 00:06:07,590 --> 00:06:10,310 Well, it's because we are calling, automatically, 78 00:06:10,310 --> 00:06:14,620 the to_s method, which we defined in the phone number class.