module RPN

Constants

VERSION

Public Class Methods

new() click to toggle source
# File lib/rpn_ruby.rb, line 9
def initialize
      @stack = []
end

Public Instance Methods

enter(value) click to toggle source
# File lib/rpn_ruby.rb, line 13
def enter value
      case value
        when '='
           return result
        when '+'
           @stack.push @stack.pop + @stack.pop
        when '-'
           subtrahend = @stack.pop
           @stack.push @stack.pop - subtrahend
        when '*'
           @stack.push @stack.pop * @stack.pop
        when '^'
           exponent = @stack.pop
           @stack.push @stack.pop ** exponent
        when 'r'
           rt = @stack.pop
           @stack.push @stack.pop.root rt
        when '/', 'd'
           divisor = @stack.pop
           @stack.push @stack.pop / divisor
        when '%'
           divisor = @stack.pop
           @stack.push @stack.pop % divisor
        else       
      @stack.push value.to_f
  end    
  self
rescue Exception => @err
  @stack.push 'invalid input'
  return self
end
result() click to toggle source
# File lib/rpn_ruby.rb, line 45
def result
      @stack.first.to_s || ''
end