Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,547 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.