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 trial

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