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!