Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Build an Address Book in Ruby!
You have completed Build an Address Book in Ruby!
Preview
Our address book functionality is getting there. In this video, we implement the ability to add contacts via the command line.
Code Samples
def run
loop do
puts "Address Book"
puts "a: Add Contact"
puts "p: Print Address Book"
puts "e: Exit"
print "Enter your choice: "
input = gets.chomp.downcase
case input
when 'a'
add_contact
when 'p'
print_contact_list
when 'e'
break
end
end
end
def add_contact
contact = Contact.new
print "First name: "
contact.first_name = gets.chomp
print "Middle name: "
contact.middle_name = gets.chomp
print "Last name: "
contact.last_name = gets.chomp
contacts.push(contact)
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
Now, if we scroll down here, we already
have this method called print_contact_list
0:00
which does exactly what we want to.
0:04
So, all we need to do is
add this as a menu option.
0:07
So we'll go ahead and
add the option to print it out.
0:13
There we go.
0:20
That will just display it on the menu.
0:21
Now we need to add this as an option.
0:24
And this is really easy,
all we need to do is say.
0:27
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