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

Nick Vitsinsky
Nick Vitsinsky
7,246 Points

Unexpected "." in the end of output

For some reason when I run contact.rb I've got

Jason Vitsinsky
Vitsinsky, Jason .
Nick A Pettit
Pettit, Nick A.

with a " ." after Jason on the second line while in the video it is without any "." Where did I made a mistake?

my code is the following

class Contact
  attr_writer :first_name, :middle_name, :last_name

  def first_name
    @first_name
  end

  def middle_name
    @middle_name
  end

  def last_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
   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
end

jason = Contact.new
jason.first_name = "Jason"
jason.middle_name = ""
jason.last_name = "Vitsinsky"
puts jason.full_name
puts jason.last_first

nick = Contact.new
nick.first_name = "Nick"
nick.middle_name = "A"
nick.last_name = "Pettit"
puts nick.full_name
puts nick.last_first

1 Answer

  if !@middle_name.nil?

You missed the @ in the def last_first ... so the program added a "." in the variable middle_name