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

Sean Flanagan
Sean Flanagan
33,219 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,219 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 :-)