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 Variables and Methods

james conigliaro
james conigliaro
3,502 Points

i am pretty lost, i am not sure if i have to add the name = Name.new, and if i have to add in title to the methods

do i create a new method called title? kind of lost on this question: In the initialize method of the Name class, set an instance variable called @title to the title argument. Note: you will have to write the initialize method.

class.rb
class Name
  def initialize(title, first_name, last_name)
    @title = title
    @first_name = first_name
    @last_name = last_name
  def title
    @title
  end
  def first_name
    @first_name
  end

  def last_name
    @last_name
  end
end
  name = Name.new("mr.", "apple", "john")

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hi James,

First off, you have added more than what the challenge is asking for, and that will most often cause a "Bummer!" even if the code is correct.

The first part wants an initialize method with "title" set... but you also added "first_name" and "last_name" which was not asked for. If you delete those you'll be good.

Also, the last part wants you to call the "title" method on the new Name (which has been assigned to the variable "name"), so you would just call the method name.title.

Below is the corrected code for you.

class Name
  def initialize (title)             #only asks for "title"
    @title = title
  end

  def title
    @title
  end

  def first_name
    "Metal"
  end

  def last_name
    "Robot"
  end
end

name = Name.new("mr.")       #creating the new instance
name.title                   #calling the "title" method on the new Name

I hope this makes sense. Keep Coding! :)

Hi there,

For the first part of the challenge - the part that you've pasted the question for - just needs an initialize method adding that takes one parameter. Then set the @title to equal the parameter:

  def initialize (title)
    @title = title
  end

That should get you on the way.

Steve.