Skip to content

Instantly share code, notes, and snippets.

@sferik
Created March 27, 2013 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sferik/5255908 to your computer and use it in GitHub Desktop.
Save sferik/5255908 to your computer and use it in GitHub Desktop.
class Menu
attr_reader :first, :last
def initialize
@first, @last = 2.times.collect{|i| prompt(i + 1)}
end
def prompt(number)
print "Enter number #{number}:\t"
gets.chomp.to_i
end
def display_result(operation, result)
puts "Result of #{operation} is:\t#{result}"
end
def clear(size)
puts "-" * (16 + size)
end
end
class Calculator
OPERATIONS = ['+', '-', '/', '*']
def initialize(menu)
@menu = menu
calculate
end
def calculate
for operation in OPERATIONS
result = eval("#{@menu.first} #{operation} #{@menu.last}") rescue "undefined"
@menu.display_result(operation, result)
end
@menu.clear(result.to_s.size.to_i)
end
end
loop do
Calculator.new(Menu.new)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment