Skip to content

Instantly share code, notes, and snippets.

@jessieay
Created June 16, 2012 04:47
Show Gist options
  • Save jessieay/2939959 to your computer and use it in GitHub Desktop.
Save jessieay/2939959 to your computer and use it in GitHub Desktop.
RPN in progress
class RPN
attr_accessor :calculator
def initialize
@calculator = []
end
def push(arg)
@calculator << arg
end
def value
ops = @calculator.last
num1 = @calculator[-3]
num2 = @calculator[-2]
result = choose_method(num1, num2, ops)
end
def choose_method(num1,num2, ops)
if ops == "+"
plus(num1, num2)
elsif ops == "-"
minus(num1, num2)
elsif ops == "*"
times(num1, num2)
elsif ops == "/"
divide(num1, num2)
end
end
def plus(num1,num2)
num1 + num2
end
# def minus(num1,num2)
# num1 - num2
# end
def minus
result = 0
@calculator.each do |i|
result -= i
end
result
end
def times(num1,num2)
num1 * num2
end
def divide(num1,num2)
num1.to_f / num2
end
end
happytesting = RPN.new
happytesting.push(2)
happytesting.push(3)
happytesting.push("*")
puts happytesting.value
# puts happytesting.calculator[-2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment