Created
March 22, 2019 12:35
-
-
Save tjmw/dd7c00ae799d19a2969312ca9b9721e6 to your computer and use it in GitHub Desktop.
Ruby proc composition example
This file contains 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
TAX_RATE = 0.4 | |
BENEFIT_COST = 1000 | |
SALARY_IN_PENCE = 15_000 * 100 | |
subtract_benefit = ->(salary) { salary - BENEFIT_COST } | |
pay_tax = ->(salary) { salary * (1 - TAX_RATE) } | |
calculate_take_home_pay = subtract_benefit >> pay_tax | |
puts "Pre-tax benefit: #{calculate_take_home_pay.call(SALARY_IN_PENCE)}" | |
sad_calculate_take_home_pay = pay_tax >> subtract_benefit | |
puts "After-tax benefit: #{sad_calculate_take_home_pay.call(SALARY_IN_PENCE)}" | |
format_salary = ->(salary) { "£#{format('%0.2f', salary / 100.0)}" } | |
pretty_take_home_pay = calculate_take_home_pay >> format_salary | |
puts "Pretty: #{pretty_take_home_pay.call(SALARY_IN_PENCE)}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment