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 Build a Bank Account Class Part 4: Printing the register

Benjamin Raskin
Benjamin Raskin
4,075 Points

Can you further explain why you have named a method 'to_s'?

I thought that this was a standard Ruby method and we shouldn't be overwriting them? Also, you never call the to_s method when you type:

puts bank_account

So how does the program know to run that?

1 Answer

I thought that this was a standard Ruby method and we shouldn't be overwriting them?

There's nothing wrong with overriding standard behavior if it lets you write your desired program more easily. That the language doesn't try to stop you is a hint that it's okay.

So how does the program know to run that?

puts knows how to check whether or not the value you pass is a string. It also knows that to_s is a standard Ruby method, and will use that to figure out how to turn bank_account into a string.