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 trialJoshua Horwitz
2,384 PointsI'm not sure what is going wrong here, everything else has been going great but I can't get this to_s to work at all.
I'm not sure why I am having so much trouble with this, when it seems as though it is just printing up 2 different instance variables.
class BankAccount
attr_reader :name
def initialize(name)
@name = name
@transactions = []
@balance = 0
add_transaction("Beginning Balance", 0)
end
def balance
@transactions.each do |transaction|
@balance += transaction[:amount]
end
return @balance
end
def debit(description, amount)
add_transaction(description, -amount)
end
def credit(description, amount)
add_transaction(description, amount)
end
def add_transaction(description, amount)
@transactions.push(description: description, amount: amount)
end
def to_s
puts "Name: #{@name}, Balance: #{BankAccount.balance}"
end
end
1 Answer
jordans
1,609 PointsMove the Balance = 0 underneath the balance method. Also, delete the "@" sign in front of the balance variable. You use a "@" sign when a variable is an instance variable. The balance variable is a local variable. Also, you're not returning the balance at the end of the balance method. Delete "return" so it only says "balance".
Next, instead of using "put" on the to_s method, use "return". You're not writing anything this time, you're returning the value. And lastly, to display the balance, just write out "Balance: #{balance}". You don't need to write the class with it.
If you want me to show you an example, just ask what you don't understand and I'll try to show you what I mean.