Skip to content

Instantly share code, notes, and snippets.

@jamiebullock
Created January 31, 2021 12:21
Show Gist options
  • Save jamiebullock/6d102e89d41be8278b107d2062f78d41 to your computer and use it in GitHub Desktop.
Save jamiebullock/6d102e89d41be8278b107d2062f78d41 to your computer and use it in GitHub Desktop.
print_balance DRY version (credit: Pragmatic Programmer)
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