Created
September 25, 2017 20:36
-
-
Save squarism/974280153d7bccba4be55b07ca052ae2 to your computer and use it in GitHub Desktop.
Calculator with paper tape side effects
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
| class Calculator | |
| # add two numbers with side effects to tape | |
| def add(number1, number2) | |
| sum = number1 + number2 | |
| tape("#{number1} + #{number2}") | |
| tape("=\n#{sum}") | |
| sum | |
| end | |
| private | |
| # print stuff to paper tape | |
| def tape(message) | |
| puts message | |
| end | |
| end | |
| calculator = Calculator.new | |
| calculator.add(1,3) | |
| # ruby calculator.rb | |
| # 1 + 3 | |
| # = | |
| # 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment