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 trialJulia Fitzer
Front End Web Development Techdegree Student 5,574 PointsPart 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?
1 Answer
Ulfar Ellenarson
5,277 PointsHI 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.
Immo Struchholz
10,515 PointsImmo Struchholz
10,515 PointsIt is calling the balance method.