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 3: Keeping Our Balance

Julia Fitzer
seal-mask
.a{fill-rule:evenodd;}techdegree
Julia Fitzer
Front End Web Development Techdegree Student 5,574 Points

Part 3: Keeping Our Balance; in Ruby Objects and Classes

In the to_s method, there's a balance variable used. Shouldn't the balance method be called on the object ? Otherwise where is the value of the balance variable coming from? (The balance variable inside of the balance method is not an instance variable). Or is it calling the balance method simply with the word balance?

Immo Struchholz
Immo Struchholz
10,515 Points

It is calling the balance method.

1 Answer

Ulfar Ellenarson
Ulfar Ellenarson
5,277 Points

HI Julia, I know this is strange as I had a similar problem with Jason's instructions regarding a very similar issue. What he is doing is using self but not telling you about it. def to_s #"Name: #{name}, Balance: #{sprintf("%0.2f", balance)}" # here we are using self.balance like bank_account.balance "Name: #{name}, Balance: #{sprintf("%0.2f", self.balance)}" end The object is bank_account which is using the puts method in the last line of the programme bank_account.rb ie. puts bank_account and this is calling the to_s method on bank_account. The object bank_account which was iinstatantiated at bank_account = BankAccount.new("Jason") is the object being referred to by self. I hope I shed some light on this for you and anybody else.