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

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,235 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. :-)