Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKevin Naegele
10,868 PointsGetting doubles Ruby address book.
I am getting doubles on my phone numbers and address... not sure why.
require "./phone_number"
require "./address"
class Contact
attr_writer :first_name, :middle_name, :last_name
attr_reader :phone_numbers, :addresses
def initialize
@phone_numbers = []
@addresses = []
end
def add_phone_number (kind, number)
phone_number = PhoneNumber.new
phone_number.kind = kind
phone_number.number = number
phone_numbers.push(phone_number)
end
def add_address(kind, street_1, street_2, city, state, postal_code)
address = Address.new
address.kind = kind
address.street_1 = street_1
address.street_2 = street_2
address.city = city
address.state = state
address.postal_code = postal_code
addresses.push(address)
end
def print_phone_numbers
puts "Phone Numbers"
phone_numbers.each { |phone_number| puts phone_number }
end
def print_addresses
puts "Addresses"
addresses.each{ |address| puts address.to_s('short') }
end
def first_name
@first_name
end
def middle_name
@middle_name
end
def last_name
@last_name
end
def first_last
first_name + " " + last_name
end
def last_first
last_first = last_name
last_first += ", "
last_first += first_name
if !@middle_name.nil?
last_first += " "
last_first += middle_name.slice(0,1)
last_first += "."
end
last_first
end
def full_name
full_name = first_name
if !@middle_name.nil?
full_name += " "
full_name += middle_name
end
full_name += ' '
full_name += last_name
full_name
end
def to_s(format = 'full_name')
case format
when 'full_name'
full_name
when 'last_first'
last_first
when 'fist_name'
fisrt_name
when 'last_name'
last_name
else
first_last
end
end
end
kevin = Contact.new
kevin.first_name = "Kevin"
kevin.middle_name = "Dan"
kevin.last_name = "Naegele"
kevin.add_phone_number("cell", "555-555-555")
kevin.add_address("Home", "1724 E Tree Dr", "", "Gilbert", "AZ", "85282" )
puts kevin.to_s('full_name')
puts kevin.print_phone_numbers
puts kevin.print_addresses
2 Answers
Ulfar Ellenarson
5,277 PointsHi the problem was with having the puts
in front of the lines kevin.to_s('full_name')
, kevin.print_phone_numbers
and kevin.print_addresses
Remove the puts
statements from the last three lines of your programme contact.rb and it will work properly.
Ulfar Ellenarson
5,277 PointsI am having the same issue. If I find out what is causing it I will post it here.