Last active
December 24, 2015 04:49
-
-
Save sudodoki/6746592 to your computer and use it in GitHub Desktop.
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
| require './calculator_class' | |
| puts "Enter numbers divided by spaces" | |
| begin | |
| # chop is used to get rid of '\n' new char line that's the last one in gets when finishing line input | |
| numbers = gets.chop.split.grep(/-?\d+/) do |possible_num| | |
| begin | |
| # unlike to_i, Integer throws exception if input is somehow invalid (letters, etc) | |
| Integer(possible_num) | |
| rescue | |
| nil | |
| end | |
| end # getting array of integers with occasional nils | |
| if numbers.length != numbers.compact.length # in case there're nils | |
| puts "Please enter a valid input" | |
| redo | |
| end | |
| if numbers.count < 2 | |
| puts "Please enter 2 or more numbers" | |
| redo | |
| end | |
| puts 'numbers are ', numbers.inspect | |
| end while false | |
| cl = Calculator.new(numbers) | |
| puts "Result of adding: #{cl.plus}" | |
| puts "Result of subtraction: #{cl.minus}" | |
| cl.clear |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment