Class: EquationSystem::System

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

Instance Method Summary collapse

Constructor Details

#initialize(variables, range: -10..10, allow_zero: false, failsafe: 10) ⇒ System

Returns a new instance of System.

Raises:



3
4
5
6
7
8
9
# File 'lib/equation_system/system.rb', line 3

def initialize(variables, range: -10..10, allow_zero: false, failsafe: 10)
  raise FailSafeError, "Failsafe cannot be less than 1" if failsafe < 1
  @prng = Random.new
  @coefficients = []
  @answers = generate_answers(variables, range, allow_zero)
  @equations = generate_equations(@answers, range, allow_zero, failsafe)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/equation_system/system.rb', line 23

def method_missing(method, *args, &block)
  match = method.to_s.match(/^solution_for_([a-z]+)$/)
  if match && @answers[match[1].to_sym]
    @answers[match[1].to_sym]
  else
    super
  end
end

Instance Method Details

#answersObject



15
16
17
# File 'lib/equation_system/system.rb', line 15

def answers
  @answers
end

#coefficientsObject



19
20
21
# File 'lib/equation_system/system.rb', line 19

def coefficients
  @coefficients
end

#equationsObject



11
12
13
# File 'lib/equation_system/system.rb', line 11

def equations
  @equations
end

#to_jsonObject



32
33
34
35
# File 'lib/equation_system/system.rb', line 32

def to_json
  require 'json' unless defined?(JSON)
  {:equations => @equations, :variables => @answers}.to_json
end