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

Task 1 is not working!

Not sure where I'm going wrong

class.rb
class Name
name = ""



  def initialize(title)
    @title = title
  end 
  def first_name
    "Metal"
  end

  def last_name
    "Robot"
  end 

  def title 
    @title = []
  end 
end

name = Name.new("Middle Name")
name = Name.title

1 Answer

Juan Gonzalez
Juan Gonzalez
12,960 Points

Hi Victor, I guess you get stucked somewhere between 3th and 4th, first of all the 3th task say "Inside the Name class, create a method called title that returns the @title variable."

and you are writting

  def title 
    @title = []
  end 

in this part, you are overriding the actual title value you assign in the initialized method with an empty array, it should be just

  def title
    @title
  end

and in the last task it say "Call the title method on the name instance." and you are writting

name = Name.title

as you are assigning a title method of the Name class (which does not actually exists) so you are getting an error, you need to call the title method of your current Name instance, that you previously assigned to the name variable, so it should look like this

name.title

the final code should look something like this

class Name
  def initialize(title)
    @title = title
  end

  def first_name
    "Metal"
  end

  def last_name
    "Robot"
  end

  def title
    @title
  end
end

name = Name.new('hello')
name.title

hope this can help you, happy coding!