class ChemistryParadise::EquationSolver

Constants

THIS_FORMULA
#

THIS_FORMULA

#
TOKEN_TO_SPLIT_AT
#

TOKEN_TO_SPLIT_AT

#

Public Class Methods

new( i = nil, run_already = true ) click to toggle source
#

initialize

#
# File lib/chemistry_paradise/equation_solver.rb, line 29
def initialize(
    i           = nil,
    run_already = true
  )
  reset
  set_input(i)
  run if run_already
end

Public Instance Methods

are_both_sides_equal?() click to toggle source
#

are_both_sides_equal?

#
# File lib/chemistry_paradise/equation_solver.rb, line 106
def are_both_sides_equal?
  result = false
  # ======================================================================= #
  # We have to eliminate the '+' characters.
  # ======================================================================= #
  left  = SplitMoleculeNames.new(left?.delete('+')).total?
  right = SplitMoleculeNames.new(right?.delete('+')).total?
  result = ( left == right )
  return result
end
ensure_that_the_token_is_correct() click to toggle source
#

ensure_that_the_token_is_correct

#
# File lib/chemistry_paradise/equation_solver.rb, line 77
def ensure_that_the_token_is_correct
  if @input.include? '=' # In this case we overrule the token.
    @token_to_split_at = '='
  end
end
left?() click to toggle source
#

left?

#
# File lib/chemistry_paradise/equation_solver.rb, line 63
def left?
  @left_hand_side
end
reset() click to toggle source
#

reset

#
# File lib/chemistry_paradise/equation_solver.rb, line 41
def reset # (reset tag)
  @token_to_split_at = TOKEN_TO_SPLIT_AT
end
right?() click to toggle source
#

right?

#
# File lib/chemistry_paradise/equation_solver.rb, line 70
def right?
  @right_hand_side
end
run() click to toggle source
#

run

#
# File lib/chemistry_paradise/equation_solver.rb, line 120
def run # (run tag)
  ensure_that_the_token_is_correct
  split_into_components
end
set_input(i = '') click to toggle source
#

set_input

#
# File lib/chemistry_paradise/equation_solver.rb, line 48
def set_input(i = '')
  i = i.first if i.is_a? Array
  i = THIS_FORMULA if i.nil?
  i = i.to_s.dup
  i.strip!
  # ======================================================================= #
  # Next, we will get rid of all ' '.
  # ======================================================================= #
  i.tr!(' ','')
  @input = i
end
split_into_components() click to toggle source
#

split_into_components

#
# File lib/chemistry_paradise/equation_solver.rb, line 86
def split_into_components
  @left_hand_side, @right_hand_side = @input.
    split(@token_to_split_at).map(&:strip)
  # ======================================================================= #
  # Ok, we now have our left side, and we have our right side.
  # We will next get the total content of Elements at the left
  # and at the right side.
  # ======================================================================= #
  p SplitMoleculeNames.new left?
  p SplitMoleculeNames.new right?
  if are_both_sides_equal?
    opn; e 'Both sides are now equal.'
  else
    opn; e 'Both sides are not equal.'
  end
end