1 00:00:00,400 --> 00:00:02,200 Okay, now comes the fun part. 2 00:00:02,200 --> 00:00:07,310 We're going to add the ability to save our address book to a file. 3 00:00:08,610 --> 00:00:11,490 Now, this is gonna work in a couple different parts. 4 00:00:12,710 --> 00:00:16,848 So for right now, let's just go ahead and write two methods, open. 5 00:00:19,468 --> 00:00:21,390 And save. 6 00:00:21,390 --> 00:00:25,500 Open and save are gonna be called at different points in the address book. 7 00:00:27,010 --> 00:00:31,760 Now, we've got this run method which basically controls our entire program. 8 00:00:31,760 --> 00:00:36,800 We could add contacts, print the address book, search through the contacts or exit. 9 00:00:38,190 --> 00:00:44,630 So before we exit, what we're gonna do is call the save method. 10 00:00:45,670 --> 00:00:50,661 What that's gonna do is save our contact list even though we haven't written 11 00:00:50,661 --> 00:00:51,208 it yet. 12 00:00:54,148 --> 00:01:00,831 And then, on the inverse of that, once we initialize the address book, 13 00:01:00,831 --> 00:01:04,886 let’s go ahead and call the open method, so 14 00:01:04,886 --> 00:01:11,260 that anything that we have saved, will be loaded in to our contacts. 15 00:01:12,750 --> 00:01:16,100 Now, all we have to do is write these methods. 16 00:01:16,100 --> 00:01:21,830 We’re going to be using a class from the standard library of Ruby, called YAML. 17 00:01:21,830 --> 00:01:28,740 What YAML will do is save all of our Ruby objects into a text-based format. 18 00:01:29,740 --> 00:01:33,368 Now, we're not gonna delve too far into YAML right now, but 19 00:01:33,368 --> 00:01:38,170 I'm going to show you a couple of different pieces of documentation so 20 00:01:38,170 --> 00:01:40,920 that we can see how YAML works. 21 00:01:40,920 --> 00:01:45,165 Basically, we get this YAML class and 22 00:01:45,165 --> 00:01:49,146 it can dump and load Ruby objects and 23 00:01:49,146 --> 00:01:54,330 it serializes them into text in a certain way. 24 00:01:55,470 --> 00:01:58,140 And then, we're going to write that text out to a file. 25 00:02:00,710 --> 00:02:05,583 Now, YAML as a class is just an interface to another class, 26 00:02:05,583 --> 00:02:10,368 which is where the methods that we'll be using come from. 27 00:02:10,368 --> 00:02:14,488 We're gonna be using the load file and 28 00:02:14,488 --> 00:02:20,410 two YAML methods, and then for writing out our files, 29 00:02:20,410 --> 00:02:25,189 we're gonna be using the IO and file class. 30 00:02:25,189 --> 00:02:30,800 We're gonna be using the open method on file which is alias for new. 31 00:02:32,130 --> 00:02:36,340 Now this is all kind of a lot to take in, so let's go ahead and write the code and 32 00:02:36,340 --> 00:02:38,270 then I will explain it. 33 00:02:40,230 --> 00:02:44,460 So we'll write our save method first, and here's how it works. 34 00:02:44,460 --> 00:02:48,970 We're gonna open a certain file, which is just gonna be called contacts.yml. 35 00:02:48,970 --> 00:02:56,160 Then, we have to tell Ruby that we're opening it with the mode w for writing. 36 00:03:00,590 --> 00:03:05,870 Then we write to this file the contents of our contacts 37 00:03:05,870 --> 00:03:10,780 array and that's gonna save our file. 38 00:03:12,040 --> 00:03:14,835 So what happens here is we're using this open method. 39 00:03:14,835 --> 00:03:20,600 File.open is a method that takes an argument, which is the name of 40 00:03:20,600 --> 00:03:25,670 the file as the first argument, and then the mode you want to access this file. 41 00:03:27,510 --> 00:03:29,510 In this case, we're choosing write mode. 42 00:03:30,660 --> 00:03:34,885 The other options that we could use are documented here in the documentation. 43 00:03:34,885 --> 00:03:39,160 Could be read-only, read-write, write-only, 44 00:03:39,160 --> 00:03:44,480 read-write, write-only and read-write, and this is only for 45 00:03:44,480 --> 00:03:49,140 appending or it depends where you want to start in the file. 46 00:03:50,170 --> 00:03:53,510 Now we're doing this as write-only which means we're going to write the contact 47 00:03:53,510 --> 00:03:55,690 list out each time we call save. 48 00:03:58,070 --> 00:04:03,088 So let's go back here and just make sure that this works. 49 00:04:06,628 --> 00:04:13,586 So first we'll add a contact, Jason Seifer, and then let's go ahead and exit. 50 00:04:13,586 --> 00:04:17,980 Uh-oh, undefined method to_yaml, and why is that? 51 00:04:19,730 --> 00:04:24,590 Well, it's because we have not required yaml. 52 00:04:24,590 --> 00:04:27,414 It's part of the standard library, 53 00:04:27,414 --> 00:04:32,708 all we have to do is require that library up here at the top of the file. 54 00:04:35,388 --> 00:04:36,688 Clear my screen here. 55 00:04:39,448 --> 00:04:40,647 And we'll try that one more time. 56 00:04:44,446 --> 00:04:48,600 Add a contact, and then let's go ahead and exit. 57 00:04:50,780 --> 00:04:52,410 Okay, so that worked. 58 00:04:52,410 --> 00:04:53,690 Now here in the work space, 59 00:04:53,690 --> 00:04:58,500 over on the left side in the side bar, go ahead and hit refresh. 60 00:05:00,040 --> 00:05:06,360 When we do that, notice that way have a file called contacts.yml. 61 00:05:06,360 --> 00:05:10,500 This is our address book program having written out contact. 62 00:05:11,660 --> 00:05:16,930 Now in yml format, this dash right here means it's an array. 63 00:05:17,970 --> 00:05:23,080 And it shows that it's a Ruby object of the contact class, and 64 00:05:23,080 --> 00:05:25,290 phone numbers and addresses are an array. 65 00:05:27,600 --> 00:05:31,616 So that looks good, we know that our contacts are being written, so 66 00:05:31,616 --> 00:05:34,018 now let's implement this open method. 67 00:05:36,918 --> 00:05:40,278 And first, we'll see if the file exists. 68 00:05:45,068 --> 00:05:50,212 So if the file exists, we're going to assume that 69 00:05:50,212 --> 00:05:55,234 we have contacts in there, so we can just replace 70 00:05:55,234 --> 00:06:00,867 the contacts array with the contents of the YAML file, 71 00:06:00,867 --> 00:06:04,806 and we do this by calling load_file. 72 00:06:08,666 --> 00:06:10,332 On the YAML class, and 73 00:06:10,332 --> 00:06:15,773 what that will do is parse everything in this file back into Ruby objects, 74 00:06:15,773 --> 00:06:21,066 and replace the current internal contacts array with those contents. 75 00:06:24,327 --> 00:06:29,586 So let me go back down here to the console, and clear the screen. 76 00:06:33,286 --> 00:06:37,677 Now when I load the address book, let's go ahead and print it out and see if there is 77 00:06:37,677 --> 00:06:41,386 anything in there, and this time the contact list has been printed. 78 00:06:41,386 --> 00:06:43,950 So here we go, let's go ahead and 79 00:06:43,950 --> 00:06:47,623 add another contact to make sure it is working. 80 00:06:47,623 --> 00:06:52,349 And let's add a phone number this time, 81 00:06:52,349 --> 00:06:56,383 and let's add an address as well. 82 00:07:11,783 --> 00:07:14,750 Okay, so all of this seems to be working. 83 00:07:14,750 --> 00:07:18,500 And now if we exit and go back over here and 84 00:07:18,500 --> 00:07:23,070 check out our contacts file, we can see that the YAML is all working correctly. 85 00:07:24,670 --> 00:07:26,780 It's loading up our contact and 86 00:07:26,780 --> 00:07:30,760 our phone numbers, and everything seems to be working. 87 00:07:30,760 --> 00:07:33,920 Now if we wanted to, we could add more things to this like the ability 88 00:07:33,920 --> 00:07:38,530 to remove contacts, but for right now, things are looking pretty good. 89 00:07:38,530 --> 00:07:43,120 And it's important to practice this stuff on your own, so if you'd like to, 90 00:07:43,120 --> 00:07:45,620 go ahead and add the ability to remove contacts. 91 00:07:45,620 --> 00:07:47,100 That would be great extra credit. 92 00:07:48,130 --> 00:07:53,290 In this course, you've built a simple but effective contact list management program. 93 00:07:53,290 --> 00:07:58,450 You've learned to use your own classes and objects, use loops and blocks and more. 94 00:07:58,450 --> 00:08:01,120 Most importantly we've learned how to break things down and 95 00:08:01,120 --> 00:08:05,040 add features to your program and think like a Ruby programmer. 96 00:08:05,040 --> 00:08:07,820 Great job, but don't stop there. 97 00:08:07,820 --> 00:08:11,340 Try to think about other things that you can add to the address book program, and 98 00:08:11,340 --> 00:08:13,090 then practice on your own by doing it.