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