Skip to content

Instantly share code, notes, and snippets.

@abrongersma
Created April 12, 2013 00:37
Show Gist options
  • Select an option

  • Save abrongersma/5368341 to your computer and use it in GitHub Desktop.

Select an option

Save abrongersma/5368341 to your computer and use it in GitHub Desktop.
class RPNCalculator
@rpn
def initialize
@rpn = []
end
def push(num)
@rpn << num
end
def plus
value1 = @rpn.pop
value2 = @rpn.pop
self.push(value2 + value1)
end
def minus
value1 = @rpn.pop
value2 = @rpn.pop
self.push(value2 - value1)
end
def times
value1 = @rpn.pop
value2 = @rpn.pop
self.push(value2 * value1)
end
def divide
value1 = @rpn.pop
value2 = @rpn.pop
self.push(value2.to_f / value1.to_f)
end
def value
@rpn.last
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment