Class: ChemistryParadise::EquationSolver

Inherits:
Base
  • Object
show all
Defined in:
lib/chemistry_paradise/utility_scripts/equation_solver.rb

Overview

require ‘equation_solver’; EquationSolver.new

Constant Summary collapse

THIS_FORMULA =
#

THIS_FORMULA

#
'NH3HHH + O2 -> N2 + H2O'
TOKEN_TO_SPLIT_AT =
#

TOKEN_TO_SPLIT_AT

#
'->'

Constants inherited from Base

Base::FILE_MOLECULAR_FORMULA_OF_DIFFERENT_MOLECULES, Base::NAMESPACE

Constants included from Shared

Shared::ARRAY_TEST_THESE_MOLECULES

Constants included from Constants

Constants::ELECTRON_NEGATIVITY_CHART, Constants::FILE_ATOMGEWICHTE, Constants::FILE_ELECTRON_NEGATIVITY_CHART, Constants::FILE_PERIODIC_TABLE_OF_THE_ELEMENTS, Constants::N, Constants::PLANK_CONSTANT, Constants::PROPER_FILLORDER, Constants::SPEED_OF_LIGHT

Instance Method Summary collapse

Methods inherited from Base

#be_quiet, #be_verbose?, #cd, #cliner, #commandline_arguments?, #do_use_the_english_language, #do_use_the_german_language, #do_we_use_english?, #esystem, #first_argument?, #gold, #grey, #initialize_the_internal_hash, #internal_hash?, #is_on_roebe?, #mediumpurple, #namespace?, #olivedrab, #opnn, #rev, #royalblue, #set_be_verbose, #set_commandline_arguments, #sfancy, #steelblue, #teal, #tomato, #use_which_language?, #yellow

Methods included from Shared

#convert_parens, #is_number?, #periodic_table?, periodic_table?, #return_range_for_this_period, #square

Methods included from Constants

#electron_negativity_chart?

Constructor Details

#initialize(i = nil, run_already = true) ⇒ EquationSolver

#

initialize

#


29
30
31
32
33
34
35
36
# File 'lib/chemistry_paradise/utility_scripts/equation_solver.rb', line 29

def initialize(
    i           = nil,
    run_already = true
  )
  reset
  set_input(i)
  run if run_already
end

Instance Method Details

#are_both_sides_equal?Boolean

#

are_both_sides_equal?

#

Returns:

  • (Boolean)


106
107
108
109
110
111
112
113
114
115
# File 'lib/chemistry_paradise/utility_scripts/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_correctObject

#

ensure_that_the_token_is_correct

#


77
78
79
80
81
# File 'lib/chemistry_paradise/utility_scripts/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?Boolean

#

left?

#

Returns:

  • (Boolean)


63
64
65
# File 'lib/chemistry_paradise/utility_scripts/equation_solver.rb', line 63

def left?
  @left_hand_side
end

#resetObject

#

reset

#


41
42
43
# File 'lib/chemistry_paradise/utility_scripts/equation_solver.rb', line 41

def reset # (reset tag)
  @token_to_split_at = TOKEN_TO_SPLIT_AT
end

#right?Boolean

#

right?

#

Returns:

  • (Boolean)


70
71
72
# File 'lib/chemistry_paradise/utility_scripts/equation_solver.rb', line 70

def right?
  @right_hand_side
end

#runObject

#

run

#


120
121
122
123
# File 'lib/chemistry_paradise/utility_scripts/equation_solver.rb', line 120

def run # (run tag)
  ensure_that_the_token_is_correct
  split_into_components
end

#set_input(i = '') ⇒ Object

#

set_input

#


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/chemistry_paradise/utility_scripts/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_componentsObject

#

split_into_components

#


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/chemistry_paradise/utility_scripts/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