Class: ExpressionInterpreter::CompositeOperation

Inherits:
Object
  • Object
show all
Defined in:
lib/expression_interpreter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCompositeOperation

Returns a new instance of CompositeOperation.



44
45
46
47
48
# File 'lib/expression_interpreter.rb', line 44

def initialize
  @operands   = []
  @operations = []
  @replaced   = "" 
end

Instance Attribute Details

#replacedObject (readonly)

Returns the value of attribute replaced.



42
43
44
# File 'lib/expression_interpreter.rb', line 42

def replaced
  @replaced
end

Instance Method Details

#add_operation(operand, operation) ⇒ Object



50
51
52
53
54
55
# File 'lib/expression_interpreter.rb', line 50

def add_operation(operand, operation)
  @operands   << operand.operation
  @operations << operation.name
  @replaced += operand.replaced
  @replaced += operation.replaced
end

#last_operation(operand) ⇒ Object



57
58
59
60
# File 'lib/expression_interpreter.rb', line 57

def last_operation(operand)
  @operands << operand.operation
  @replaced += operand.replaced
end

#operationObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/expression_interpreter.rb', line 62

def operation
%w(* / + -).each do |op_match|
  i = 0  
  retry if @operations.detect do |operation|
    if op_match == operation
      @operands[i] = Operation.new(@operands[i], @operations[i], @operands[i+1])
      @operands.delete_at(i+1)
      @operations.delete_at(i)
      true
    else
      i += 1
      false
    end
  end
end
@operands[0]
end

#to_sObject



80
81
# File 'lib/expression_interpreter.rb', line 80

def to_s
end