Skip to content

Instantly share code, notes, and snippets.

@jmoon90
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save jmoon90/9498666 to your computer and use it in GitHub Desktop.

Select an option

Save jmoon90/9498666 to your computer and use it in GitHub Desktop.
@number = 0
print "How much should we add:"
@number + gets.chomp.to_i
puts "> #{@number}"
print "How much should we subtract:"
@number - gets.chomp.to_i
puts "> #{@number}"
print "How much should we multiply:"
@number = gets.chomp.to_i * @number
puts "> #{@number}"
print "How much should we divide:"
@number = @number / gets.chomp.to_i
puts "> #{@number}"
print "Would you like to add again?"
puts answer = gets.chomp
if answer == "yes"
calculator.rb
else
return
end
class Calculator
def initialize
@number = 0
end
def add
print "How much should we add:"
n = gets.chomp.to_i
@number = n + @number
puts "> #{@number}"
end
def subtract
print "How much should we subtract:"
n = gets.chomp.to_i
@number = @number - n
puts "> #{@number}"
end
def multiply
print "How much should we multiply:"
n = gets.chomp.to_i
@number = n * @number
puts "> #{@number}"
end
def divide
print "How much should we divide:"
n = gets.chomp.to_i
@number = @number / n
puts "> #{@number}"
end
def repeat
print "Would you like to add again?"
answer = gets.chomp
if answer == "yes"
run_file
else
return
end
end
def run_file
add
subtract
multiply
divide
repeat
end
end
calc = Calculator.new
calc.run_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment