Created
January 31, 2021 12:21
-
-
Save jamiebullock/6d102e89d41be8278b107d2062f78d41 to your computer and use it in GitHub Desktop.
print_balance DRY version (credit: Pragmatic Programmer)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def format_amount(value) | |
result = sprintf("%10.2f", value.abs) | |
if value < 0 | |
result + "-" | |
else | |
result + " " | |
end | |
end | |
def print_line(label, value) | |
printf "%-9s%s\n", label, value | |
end | |
def report_line(label, amount) | |
print_line(label + ":", format_amount(amount)) | |
end | |
def print_balance(account) | |
report_line("Debits", account.debits) | |
report_line("Credits", account.credits) | |
report_line("Fees", account.fees) print_line("", "———-") | |
report_line("Balance", account.balance) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment