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

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