Created
March 27, 2013 16:47
-
-
Save sferik/5255908 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
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