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

Inside the Name class, create a method called title that returns the @title variable.

What am I doing wrong here? I'm a little confused.

class.rb
class Name
  def initialize(title)
    @title = "title"
  end

  def title
    @title
  end

  def first_name
    "Metal"
  end

  def last_name
    "Robot"
  end
end

name = Name.new("Corey")
peyton caseria
peyton caseria
Courses Plus Student 12,281 Points

all you need to do is remove the string around title on line 3,

and you can remove def first & last name,

and then at the bottom add puts name.title after the Name.new onto line 20

1 Answer

peyton caseria
PLUS
peyton caseria
Courses Plus Student 12,281 Points

This passed part 1-4

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

  def title
    @title
  end
end

name  = Name.new("Mr.")
puts name.title

one of the reasons this works is because initialize is a saved def method, This is a little tricky to explain.

So, the Class Name has a def "method" of initializer(title) The def Initialize and def title are both within the Class Name block.

When we do

name = Name.new("Mr.") 

We use the puts name.title to print out the new name of ("Mr.")

With the @title = title inside the method initialize we are waiting for this method to hit the instance variable.

So, we are calling the def title and the instance variable @title = title .

The final piece to this puzzle is the puts name.title

this prints out our name which is new as ("Mr.")

then we add .title which will put the name and title name together onto the screen.

If I made a mistake explaining this, someone let me know.

Thanks. ~Peyton