Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Saving
Documentation Links
- Ruby IO Documentation
- Ruby YAML Documentation
- Ruby Psych Documentation - This is the class YAML interfaces with.
Code Samples
require "./contact"
require "yaml"
class AddressBook
attr_reader :contacts
def initialize
@contacts = []
open()
end
def open
if File.exist?("contacts.yml")
@contacts = YAML.load_file("contacts.yml")
end
end
def save
File.open("contacts.yml", "w") do |file|
file.write(contacts.to_yaml)
end
end
def run
loop do
puts "Address Book"
puts "a: Add Contact"
puts "p: Print Address Book"
puts "s: Search"
puts "e: Exit"
print "Enter your choice: "
input = gets.chomp.downcase
case input
when 'a'
add_contact
when 'p'
print_contact_list
when 's'
print "Search term: "
search = gets.chomp
find_by_name(search)
find_by_phone_number(search)
find_by_address(search)
when 'e'
save()
break
end
puts "\n"
end
end
-
0:00
Okay, now comes the fun part.
-
0:02
We're going to add the ability to save our address book to a file.
-
0:08
Now, this is gonna work in a couple different parts.
-
0:12
So for right now, let's just go ahead and write two methods, open.
-
0:19
And save.
-
0:21
Open and save are gonna be called at different points in the address book.
-
0:27
Now, we've got this run method which basically controls our entire program.
-
0:31
We could add contacts, print the address book, search through the contacts or exit.
-
0:38
So before we exit, what we're gonna do is call the save method.
-
0:45
What that's gonna do is save our contact list even though we haven't written
-
0:50
it yet.
-
0:54
And then, on the inverse of that, once we initialize the address book,
-
1:00
let’s go ahead and call the open method, so
-
1:04
that anything that we have saved, will be loaded in to our contacts.
-
1:12
Now, all we have to do is write these methods.
-
1:16
We’re going to be using a class from the standard library of Ruby, called YAML.
-
1:21
What YAML will do is save all of our Ruby objects into a text-based format.
-
1:29
Now, we're not gonna delve too far into YAML right now, but
-
1:33
I'm going to show you a couple of different pieces of documentation so
-
1:38
that we can see how YAML works.
-
1:40
Basically, we get this YAML class and
-
1:45
it can dump and load Ruby objects and
-
1:49
it serializes them into text in a certain way.
-
1:55
And then, we're going to write that text out to a file.
-
2:00
Now, YAML as a class is just an interface to another class,
-
2:05
which is where the methods that we'll be using come from.
-
2:10
We're gonna be using the load file and
-
2:14
two YAML methods, and then for writing out our files,
-
2:20
we're gonna be using the IO and file class.
-
2:25
We're gonna be using the open method on file which is alias for new.
-
2:32
Now this is all kind of a lot to take in, so let's go ahead and write the code and
-
2:36
then I will explain it.
-
2:40
So we'll write our save method first, and here's how it works.
-
2:44
We're gonna open a certain file, which is just gonna be called contacts.yml.
-
2:48
Then, we have to tell Ruby that we're opening it with the mode w for writing.
-
3:00
Then we write to this file the contents of our contacts
-
3:05
array and that's gonna save our file.
-
3:12
So what happens here is we're using this open method.
-
3:14
File.open is a method that takes an argument, which is the name of
-
3:20
the file as the first argument, and then the mode you want to access this file.
-
3:27
In this case, we're choosing right mode.
-
3:30
The other options that we could use are documented here in the documentation.
-
3:34
Could be read-only, read-write, write-only,
-
3:39
read-write, write-only and read-write, and this is only for
-
3:44
a pending or it depends where you want to start in the file.
-
3:50
Now we're doing this as write-only which means we're going to write the contact
-
3:53
list out each time we call save.
-
3:58
So let's go back here and just make sure that this works.
-
4:06
So first we'll add a contact, Jason Seifer, and then let's go ahead and exit.
-
4:13
Uh-oh, undefined method to_yaml, and why is that?
-
4:19
Well, it's because we have not required ymml.
-
4:24
It's part of the standard library,
-
4:27
all we have to do is require that library up here at the top of the file.
-
4:35
Clear my screen here.
-
4:39
And we'll try that one more time.
-
4:44
Add a contact, and then let's go ahead and exit.
-
4:50
Okay, so that worked.
-
4:52
Now here in the work space,
-
4:53
over on the left side in the side bar, go ahead and hit refresh.
-
5:00
When we do that, notice that way have a file called contacts.yml.
-
5:06
This is our address book program having written out contact.
-
5:11
Now in yml format, this dash right here means it's an array.
-
5:17
And it shows that it's a Ruby object of the contact class, and
-
5:23
phone numbers and addresses are an array.
-
5:27
So that looks good, we know that our contacts are being written, so
-
5:31
now let's implement this open method.
-
5:36
And first, we'll see if the file exists.
-
5:45
So if the file exists, we're going to assume that
-
5:50
we have contacts in there, so we can just replace
-
5:55
the contacts array with the contents of the YAML file,
-
6:00
and we do this by calling load_file.
-
6:08
On the YAML class, and
-
6:10
what that will do is parse everything in this file back into Ruby objects,
-
6:15
and replace the current internal contacts array with those contents.
-
6:24
So let me go back down here to the console, and clear the screen.
-
6:33
Now when I load the address book, let's go ahead and print it out and see if there is
-
6:37
anything in there, and this time the contact list has been printed.
-
6:41
So here we go, let's go ahead and
-
6:43
add another contact to make sure it is working.
-
6:47
And let's add a phone number this time,
-
6:52
and let's add an address as well.
-
7:11
Okay, so all of this seems to be working.
-
7:14
And now if we exit and go back over here and
-
7:18
check out our contacts file, we can see that the YAML is all working correctly.
-
7:24
It's loading up our contact and
-
7:26
our phone numbers, and everything seems to be working.
-
7:30
Now if we wanted to, we could add more things to this like the ability
-
7:33
to remove contacts, but for right now, things are looking pretty good.
-
7:38
And it's important to practice this stuff on your own, so if you'd like to,
-
7:43
go ahead and add the ability to remove contacts.
-
7:45
That would be great extra credit.
-
7:48
In this course, you've built a simple but effective contact list management program.
-
7:53
You've learned to use your own classes and objects, use loops and blocks and more.
-
7:58
Most importantly we've learned how to break things down and
-
8:01
add features to your program and think like a Ruby programmer.
-
8:05
Great job, but don't stop there.
-
8:07
Try to think about other things that you can add to the address book program, and
-
8:11
then practice on your own by doing it.
You need to sign up for Treehouse in order to download course files.
Sign up