Module: RPN

Defined in:
lib/rpn_ruby.rb,
lib/rpn_ruby/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#enter(value) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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

#initializeObject



9
10
11
# File 'lib/rpn_ruby.rb', line 9

def initialize
	@stack = []
end

#resultObject



45
46
47
# File 'lib/rpn_ruby.rb', line 45

def result
	@stack.first.to_s || ''
end