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

Sean Flanagan
Sean Flanagan
33,218 Points

Syntax error

Hi. I have a keyword_end error in my syntax, on line 69:

class Contact
  attr_writer :forename, :middle_name, :surname

  def forename
    @forename
  end

  def middle_name
    @middle_name
  end

  def surname
    @surname
  end

  def forename_first
    forename + " " + 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

  def to_s(format = "full_name")
    case format
    when "full_name"
      full_name
    when "surname_first"
      surname_first
    when "first"
      forename
    when "last"
      surname
    else
      surname_first
  end
end

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

marcia = Contact.new
marcia.forename = "Marcia"
marcia.surname = "Scott"
puts marcia.to_s("forename_first") //keyword_end error here

Does anyone know where I've gone wrong please?

Thanks. :-)

Juan Ordaz
Juan Ordaz
12,012 Points

I could be wrong... But beside missing an "end" keyword after surname_first at the end of the method "to_s", you have a bug -- calling a format you haven't write (located at the last line of your code) in your case statement, inside the "to_s method". I think what you meant to pass as a argument was "puts marcia.to_s("full_name")" instead of "puts marcia.to_s("forename_first")"... When you run this code on your terminal it does not throw an error but it does not do what you think is doing, which is returning the first name.

1 Answer

Thomas Perkins
Thomas Perkins
496 Points

put "end"

You seem to have left out an "end" that's about all I can see.

Sean Flanagan
Sean Flanagan
33,218 Points

Hi Thomas. Thanks for your input. I think I can see where I've missed an "end" statement: after "surname_first"? I've just typed it and now the program works.

Cheers. :-)