Module: CalculationHelper
- Included in:
- UnitManager::Unit
- Defined in:
- lib/util/calculation_helper.rb
Instance Method Summary collapse
- #addition(split_equation:, index:) ⇒ Object
- #calculation_equation(split_equation:) ⇒ Object
- #division(split_equation:, index:) ⇒ Object
- #multiplication(split_equation:, index:) ⇒ Object
- #subtraction(split_equation:, index:) ⇒ Object
Instance Method Details
#addition(split_equation:, index:) ⇒ Object
51 52 53 54 55 |
# File 'lib/util/calculation_helper.rb', line 51 def addition(split_equation:, index:) result = split_equation[index-1].to_f + split_equation[index+1].to_f split_equation.slice!(index, 2) split_equation[index-1] = result.to_s end |
#calculation_equation(split_equation:) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/util/calculation_helper.rb', line 2 def calculation_equation(split_equation:) while split_equation.index('*').present? || split_equation.index('/').present? do multiplication_index = split_equation.index('*').presence || UnitManager::Unit::MAX_EQUATION_SIZE division_index = split_equation.index('/').presence || UnitManager::Unit::MAX_EQUATION_SIZE if multiplication_index <= division_index multiplication(split_equation: split_equation, index: multiplication_index) next end if division_index < multiplication_index division(split_equation: split_equation, index: division_index) next end end while split_equation.index('+').present? || split_equation.index('-').present? do addition_index = split_equation.index('+').presence || UnitManager::Unit::MAX_EQUATION_SIZE subtraction_index = split_equation.index('-').presence || UnitManager::Unit::MAX_EQUATION_SIZE if addition_index <= subtraction_index addition(split_equation: split_equation, index: addition_index) next end if subtraction_index < addition_index subtraction(split_equation: split_equation, index: subtraction_index) next end end result = split_equation[0] result.to_f.round end |
#division(split_equation:, index:) ⇒ Object
45 46 47 48 49 |
# File 'lib/util/calculation_helper.rb', line 45 def division(split_equation:, index:) result = split_equation[index-1].to_f / split_equation[index+1].to_f split_equation.slice!(index, 2) split_equation[index-1] = result.to_s end |
#multiplication(split_equation:, index:) ⇒ Object
39 40 41 42 43 |
# File 'lib/util/calculation_helper.rb', line 39 def multiplication(split_equation:, index:) result = split_equation[index-1].to_f * split_equation[index+1].to_f split_equation.slice!(index, 2) split_equation[index-1] = result.to_s end |
#subtraction(split_equation:, index:) ⇒ Object
57 58 59 60 61 |
# File 'lib/util/calculation_helper.rb', line 57 def subtraction(split_equation:, index:) result = split_equation[index-1].to_f - split_equation[index+1].to_f split_equation.slice!(index, 2) split_equation[index-1] = result.to_s end |