Class: ReversePolishCalculator::Stack
- Inherits:
-
Object
- Object
- ReversePolishCalculator::Stack
- Defined in:
- lib/reverse-polish-calculator/stack.rb
Instance Attribute Summary collapse
-
#aggregate ⇒ Object
readonly
Returns the value of attribute aggregate.
-
#inputs ⇒ Object
readonly
Returns the value of attribute inputs.
Instance Method Summary collapse
- #add(expression) ⇒ Object
- #calculate ⇒ Object
-
#initialize ⇒ Stack
constructor
A new instance of Stack.
- #output ⇒ Object
- #remove(input_value) ⇒ Object
- #unswap ⇒ Object
Constructor Details
Instance Attribute Details
#aggregate ⇒ Object (readonly)
Returns the value of attribute aggregate.
4 5 6 |
# File 'lib/reverse-polish-calculator/stack.rb', line 4 def aggregate @aggregate end |
#inputs ⇒ Object (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 |
#calculate ⇒ Object
22 23 24 |
# File 'lib/reverse-polish-calculator/stack.rb', line 22 def calculate @aggregate = @inputs.calculate(aggregate) end |
#output ⇒ Object
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 |
#unswap ⇒ Object
33 34 35 |
# File 'lib/reverse-polish-calculator/stack.rb', line 33 def unswap inputs.swapped = false end |