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 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.