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

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

attr_writer class

class Name

  attr_reader(:title, :first, :middle, :Last)
  attr_writer(:title)

  def initialize(title, first, middle, last)
    @title = title
    @first = first
    @middle = middle
    @last = last
  end
end

name = Name.new ("Mr", "Ally", "", "Cuccurullo")

puts name.title = " " +
     name.first + " " +
     name.middle+ " " +
     name.last

     puts "title: #{name.title} "
     name.title = "dr"
     puts "title: #{name.title} "

So its telling me syntax error unexpected ',' for rb:16

3 Answers

Tim Knight
Tim Knight
28,888 Points

Alphonse,

It seems your issue is likely the space between Name.new and the (.

Give this a shot:

name = Name.new("Mr", "Ally", "", "Cuccurullo")
Kirby Ziada
Kirby Ziada
9,886 Points

From at first glance, your issue is with the attr_reader and attr_writer

  attr_reader(:title, :first, :middle, :Last)
  attr_writer(:title)

You've got parenthesis that should not be there, you've also got a capital L in last and you've got :title in both reader and writer, it should be in just writer (or accessor if you've reached that part).

Instead, your code should read.

class Name

  attr_reader :first, :middle, :last
  attr_accessor :title

  def initialize(title, first, middle, last)
    @title = title
    @first = first
    @middle = middle
    @last = last
  end
end

name = Name.new ("Mr", "Ally", "", "Cuccurullo")

puts name.title = " " +
     name.first + " " +
     name.middle+ " " +
     name.last

     puts "title: #{name.title} "
     name.title = "dr"
     puts "title: #{name.title} "
Alex Kharouk
Alex Kharouk
4,411 Points

I know this is old, but I wanted to just see if those solutions actually solved the problem. The solution, I think, is actually where it says

puts name.title = " " +
     name.first + " " +
     name.middle+ " " +
     name.last

I think you meant to put a '+' rather than the '=' . That should fix it, no?