Skip to content

Instantly share code, notes, and snippets.

@sudodoki
Last active December 24, 2015 04:49
Show Gist options
  • Select an option

  • Save sudodoki/6746592 to your computer and use it in GitHub Desktop.

Select an option

Save sudodoki/6746592 to your computer and use it in GitHub Desktop.
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