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 The to_s Method

where am I going wrong here

Create a to_s method for Name that returns the first_name and last_name separated by a space.

2 Answers

Hi James,

Here's to_s method solution.

  def to_s
    "#{first_name} #{last_name}" 
  end

Hope that helps.

thank you Salman

Great. Just note that we encourage you to select best answer option to let other members know solutions available to the question. :)

Tiru Otilia
Tiru Otilia
2,274 Points
class Name
  attr_reader :first_name, :last_name

  def initialize(first_name, last_name)
    @first_name = first_name
    @last_name = last_name
  end

  def full_name
    first_name + ' ' + last_name
  end
def to_s
  full_name
end
end