Class: ReversePolishCalculator::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/reverse-polish-calculator/stack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStack

Returns a new instance of Stack.



6
7
8
# File 'lib/reverse-polish-calculator/stack.rb', line 6

def initialize
  @inputs, @aggregate = [].extend(Inputs), 0
end

Instance Attribute Details

#aggregateObject (readonly)

Returns the value of attribute aggregate.



4
5
6
# File 'lib/reverse-polish-calculator/stack.rb', line 4

def aggregate
  @aggregate
end

#inputsObject (readonly)

Returns the value of attribute inputs.



4
5
6
# File 'lib/reverse-polish-calculator/stack.rb', line 4

def inputs
  @inputs
end

Instance Method Details

#add(expression) ⇒ Object



10
11
12
13
14
# File 'lib/reverse-polish-calculator/stack.rb', line 10

def add(expression)
  expression.split.each do |value|
    @inputs << Input.new(value)
  end
end

#calculateObject



22
23
24
# File 'lib/reverse-polish-calculator/stack.rb', line 22

def calculate
  @aggregate = @inputs.calculate(aggregate)
end

#outputObject



26
27
28
29
30
31
# File 'lib/reverse-polish-calculator/stack.rb', line 26

def output
  Output.add "stack: #{@inputs.inspect}"
  Output.add "aggregate: #{@aggregate}"
  Output.display
  Output.clear
end

#remove(input_value) ⇒ Object



16
17
18
19
20
# File 'lib/reverse-polish-calculator/stack.rb', line 16

def remove(input_value)
  @inputs.delete_if do |input|
    input.value == input_value.to_s
  end
end

#unswapObject



33
34
35
# File 'lib/reverse-polish-calculator/stack.rb', line 33

def unswap
  inputs.swapped = false
end