Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
A bank account is made up of transactions. In this video, we're going to create methods to add transactions to our bank account. The transactions can be credits or debits and affect the running balance.
Code Samples
class BankAccount
attr_reader :name
def initialize(name)
@name = name
@transactions = []
add_transaction("Beginning Balance", 0)
end
def credit(description, amount)
add_transaction(description, amount)
end
def debit(description, amount)
add_transaction(description, -amount)
end
def add_transaction(description, amount)
@transactions.push(description: description, amount: amount)
end
end
bank_account = BankAccount.new("Jason")
bank_account.credit("Paycheck", 100)
bank_account.debit("Groceries", 40)
puts bank_account.inspect
Returns the following:
#<BankAccount:0x007f00882acbc0
@name="Jason",
@transactions=[
{:description=>"Beginning Balance", :amount=>0},
{:description=>"Paycheck", :amount=>100},
{:description=>"Groceries", :amount=>-40}
]>
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Mateusz Leśniak
4,762 Points3 Answers
-
Richard McGrath
5,597 Points2 Answers
-
Colin Xu
4,967 Points1 Answer
-
Eliza SJ
4,587 PointsI'm confused about hashes. Where do the symbols come from and why are they needed here ?
Posted by Eliza SJEliza SJ
4,587 Points3 Answers
-
Jordano Moscoso
3,104 Points3 Answers
-
Adrian Yeow
13,866 Points3 Answers
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up