Created
July 16, 2013 04:51
-
-
Save shaohua/6005862 to your computer and use it in GitHub Desktop.
wip for ruby
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 add(*numbers) | |
| numbers.inject(0) {|sum, number| sum += number} | |
| end | |
| def subtract(*numbers) | |
| numbers.inject {|result, number| result -= number} | |
| end | |
| def calculate(*args) | |
| options = {'add'=>true} | |
| if(args.last.is_a?(Hash)) | |
| options = args.last | |
| args.delete_at(-1) | |
| end | |
| (return add(*args)) if options['add'] | |
| (return subtract(*args)) if options['subtract'] | |
| end | |
| # p add(1,2) | |
| # p add([1,2]) | |
| p calculate(1,2,{'subtract'=>true}) | |
| p calculate(1,2,3,{'subtract'=>true}) | |
| p calculate(1,2, {'add'=>true}) | |
| p calculate(1,2,3,4, {'add'=>true}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment