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

Sean Flanagan
Sean Flanagan
33,235 Points

Different output

Hi. My surname_first method isn't showing in the console. Does anyone know why?

Here's my contact.rb:

class Contact
  attr_writer :forename, :middle_name, :surname

  def forename
    @forename
  end

  def middle_name
    @middle_name
  end

  def surname
    @surname
  end

  def surname_first
    surname_first = surname
    surname_first = ", "
    surname_first = forename
    if !middle_name.nil?
      surname_first += " "
      surname_first += middle_name.slice(0, 1)
      surname_first = "."
    end
    surname_first
  end

  def full_name
    full_name = forename
    if !middle_name.nil?
      full_name += " "
      full_name += middle_name
    end
    full_name += ' '
    full_name += surname
    full_name
  end
end

sean = Contact.new
sean.forename = "Sean"
sean.middle_name = "Michael"
sean.surname = "Flanagan"
puts sean.full_name
puts sean.surname_first

marcia = Contact.new
marcia.forename = "Marcia"
marcia.surname = "Scott"
puts marcia.full_name
puts marcia.surname_first

Thanks.

2 Answers

Taylor Boudreau
Taylor Boudreau
7,285 Points

Hey there Michael,

In your surname_first method, it looks like you're reassigning the value of your surname_first local variable rather than using the += operator. I believe I've achieved the desired result below.

def surname_first
    surname_first = surname
    surname_first += ", "
    surname_first += forename
    if !middle_name.nil?
      surname_first += " "
      surname_first += middle_name.slice(0, 1)
      surname_first += "."
    end
    surname_first
 end

Hope this helps!

Taylor

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Taylor.

Firstly, let me politely point out that Michael is my middle name, not my first name, as denoted by this line:

sean.middle_name = "Michael"

Secondly, however, your contribution worked, so thanks.

Sean :-)