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

I'm still confused why calling to_s is needed.

Could someone walk me through an example of what happens when you don't call to_s versus when you do?

https://teamtreehouse.com/library/the-tos-method

1 Answer

# to_s returns a string representation of an object
# you don't necessary need to call it, it automatically get called when you use puts
# for example

class Test
end

puts Test.new
puts Test.new.to_s
# outputs
# <Test:0x0000000001244d10>                                                                                
# <Test:0x0000000001244b30>
# just what we should expect, string representation of an object
# The main point of the video that you should get is that
# to_s is a method of the object
# and that we can redefine how to_s returns something

# Hopefully this helps you somehow :\

ohh I see. thank you!