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 Ruby Objects and Classes Variables and Methods Attribute Writers and Accessors

Sean Flanagan
Sean Flanagan
33,235 Points

Error message

Hi. I've got as far as 3 minutes 30 seconds into the video. There seems to be a problem with my syntax.

class Name
  attr_reader :title, :forename, :middle_name, :surname

  def initialize(title, forename, middle_name, surname)
    @title = title
    @forename = forename
    @middle_name = middle_name
    @surname = surname
  end

  def title = (new_title)
    @title = new_title
  end

end

name = Name.new("Mr.", "Sean", "Michael", "Flanagan")
puts name.title + " " +
     name.forename + " " +
     name.middle_name + " " +
     name.surname

puts "Title: #{name.title}"
name.title = "Dr."
puts "Title: #{name.title}"

Error:

name.rb:11: syntax error, unexpected '=', expecting ';' or '\n'                
  def title = (new_title)                                                      
             ^                                                                 
name.rb:15: syntax error, unexpected keyword_end, expecting end-of-input  

Does anyone know why my code won't pass please? Thanks. :-)

Jeremy Faith
Jeremy Faith
Courses Plus Student 56,696 Points

Which Objective are you on? Did you use attr_writer :first_name and def title = (new_title) @title = new_title end? This may be the cause of your error.

2 Answers

# You need to remove the spaces here.
  def title=(new_title)
    @title = new_title
  end

Hope this helps!

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Jeremy. Hi Charles. Just to let you know I've deleted the spaces between title and =, and between = and the opening parenthesis, as per your suggestion Charles and the program is now working. Thank you both. :-)

Sweet! Glad I could help.