Ruby Build an Address Book in Ruby Class Design Address Class: Part 2

Kevin Naegele
Kevin Naegele
10,868 Points

Getting 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
Ulfar Ellenarson
5,277 Points

Hi 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
Ulfar Ellenarson
5,277 Points

I am having the same issue. If I find out what is causing it I will post it here.