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 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
loop do
puts "Add phone number or address? "
puts "p: Add phone number"
puts "a: Add address"
puts "(Any other key to go back)"
response = gets.chomp.downcase
case response
when 'p'
phone = PhoneNumber.new
print "Phone number kind (Home, Work, etc): "
phone.kind = gets.chomp
print "Number: "
phone.number = gets.chomp
contact.phone_numbers.push(phone)
when 'a'
address = Address.new
print "Address Kind (Home, Work, etc): "
address.kind = gets.chomp
print "Address line 1: "
address.street_1 = gets.chomp
print "Address line 2: "
address.street_2 = gets.chomp
print "City: "
address.city = gets.chomp
print "State: "
address.state = gets.chomp
print "Postal Code: "
address.postal_code = gets.chomp
contact.addresses.push(address)
else
print "\n"
break
end
end
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, what we're gonna do is add
the ability to add addresses and
0:00
phone numbers to this
contact when we add them.
0:04
So, we're gonna do that right here and
we're gonna do this using another loop.
0:08
So we'll say loop do and end.
0:13
So now we'll just display another menu, so
0:18
when we add this contact we'll
say add phone number or address.
0:22
And then we can use the letter
p to add a phone number
0:29
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