Class: Belir::System

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

Overview

Represents a set or System of Equations

Instance Method Summary collapse

Constructor Details

#initialize(*equations) ⇒ System

Returns a new instance of System.



25
26
27
# File 'lib/belir.rb', line 25

def initialize(*equations)
  @equations = equations
end

Instance Method Details

#add_equation(eqn) ⇒ Object



29
30
31
# File 'lib/belir.rb', line 29

def add_equation(eqn)
  @equations.push(eqn)
end

#solve(vars) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/belir.rb', line 33

def solve(vars)
  begin
    found = false
    @equations.each do |eqn|
      if vars[eqn.output].nil? && eqn.inputs.all? { |input| vars.key? input }
        args = []
        eqn.inputs.each do |input|
          args.push vars[input]
        end
        vars[eqn.output] = eqn.calculate(*args) # Splat to prevent [[2, 3]]
        found = true
      end
    end
  end while found
  vars
end