class ChemistryParadise::CalculateAtomicMass

Constants

ARRAY_HELP_OPTIONS
#

ARRAY_HELP_OPTIONS

#
ATOMGEWICHTE
DEFAULT_INPUT
#

ChemistryParadise::CalculateAtomicMass::DEFAULT_INPUT

#
NAMESPACE
#

NAMESPACE

#

Public Class Methods

[](i) click to toggle source
#

CalculateAtomicMass[]

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 413
def self.[](i)
  parse(i)
end
new( optional_input = ARGV, run_already = true ) click to toggle source
#

initialize

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 48
def initialize(
    optional_input = ARGV,
    run_already    = true
  )
  reset
  set_input(optional_input)
  case run_already.to_s
  when 'do_not_report'
    @report_result = false
    run_already = true
  end
  run if run_already
end
parse(i) click to toggle source
#

CalculateAtomicMass.parse

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 420
def self.parse(i)
  _ = CalculateAtomicMass.new(i, false)
  _.calculate
  return _.nice_result
end

Public Instance Methods

atomgewichte?() click to toggle source
#

atomgewichte?

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 179
def atomgewichte?
  ATOMGEWICHTE
end
ba(i)
Alias for: calculate_compound
bold_red(i) click to toggle source
#

bold_red

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 208
def bold_red(i)
  swarn(i)
end
calculate()
Alias for: calculate_result
calculate_compound(i) click to toggle source
#

calculate_compound

This method will attempt to calculate the given input. The input to this method will usually be something like “Pb3”

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 162
def calculate_compound(i)
  result = 0
  if i =~ /\d+/ # if input has a number
    splitted = i.split(/(\d+)/)
  else # else assume that the number is one.
    splitted = [ i, 1 ]
  end
  if atomgewichte?.has_key? splitted[0]
    result = splitted[1].to_i * ATOMGEWICHTE[splitted[0]]
  end
  gather_individual_components(i, result)
  return result
end
Also aliased as: ba
calculate_result() click to toggle source
#

calculate_result

Use this method to calculate the result (the molecular mass).

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 371
def calculate_result
  mass_number = 0
  splitted = SplitMoleculeNames.new(input?).result
  splitted = sanitize_input_for_element_names(splitted)
  splitted.each {|entry|
    add_this_amount = calculate_compound(entry)
    if @show_the_steps
      e "#{rev}Now adding #{royalblue(add_this_amount)} for the "\
        "compound #{steelblue(entry)}."
    end
    mass_number += add_this_amount
  }
  set_result(mass_number)
  determine_output_string
end
Also aliased as: calculate
check_against_menu(i = @commandline_arguments)
Alias for: menu
consider_modifying_the_output_string() click to toggle source
#

consider_modifying_the_output_string

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 328
def consider_modifying_the_output_string
  if @show_more # This can modify the output string.
    _ = ''.dup
    Hash[@individual_components].each_pair {|key, value|
      key = key.scan(/./).map {|entry|
        if entry =~ /^\d+/ # Starts with a number.
          entry << 'x'
        end
        entry
      }
      key = key.join
      _ << key+': '+value.round(7).to_s+' | '
    }
    @output_string = @output_string.dup if @output_string.frozen?
    @output_string << "#{N}#{bold_red('  (The individual components were: ').dup}#{rev}"
    _.strip!
    splitted = _.split('|').map {|entry|
      Colours.kde_colour_palette_plasma_blue(entry)
    }
    joined = splitted.join(teal('|'))
    @output_string << joined+
                      bold_red(')')
  end
end
determine_output_string() click to toggle source
#

determine_output_string

This method will determine the String that will be shown to the user on the commandline.

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 306
def determine_output_string
  _ = nice_result
  if do_we_use_english?
    @output_string = "The molecular mass of #{sfancy(@input)} is "\
                     "#{simp(_+' u (g / mol)')}."
  else # then we probably use german
    @output_string = "#{rev}Die molekulare Masse von #{sfancy(@input)} "\
                     "beträgt #{simp(_+' u (g / mol)')}."
  end
end
do_show_details() click to toggle source
#

do_show_details

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 289
def do_show_details
  @show_more = true
end
do_show_the_steps() click to toggle source
#

do_show_the_steps

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 390
def do_show_the_steps
  @show_the_steps = true
end
gather_individual_components(name, weight) click to toggle source
#

gather_individual_components

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 200
def gather_individual_components(name, weight)
  array = [name, weight]
  @individual_components << array
end
include?(i = input?)
Alias for: is_included?
input?() click to toggle source
#

input?

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 129
def input?
  @input
end
is_included?(i = input?) click to toggle source
#

is_included?

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 222
def is_included?(i = input?)
  i = i[0,2] if i.size > 2 # Take only the first two characters, in the event that we have more than two characters in the supplied input.
  atomgewichte?.has_key?(i)
end
Also aliased as: include?
masse?()
Alias for: result?
menu(i = @commandline_arguments) click to toggle source
#

menu (menu tag)

The @commandline_arguments will be checked against the internal menu.

#
Also aliased as: check_against_menu
nice_result() click to toggle source
#

nice_result

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 215
def nice_result
  @result.to_f.round(3).to_s
end
opnn() click to toggle source
#

opnn

#
Calls superclass method ChemistryParadise::Base#opnn
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 296
def opnn
  super(NAMESPACE)
end
report_result() click to toggle source
#

report_result

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 193
def report_result
  e @output_string
end
report_result?() click to toggle source
#

report_result?

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 238
def report_result?
  @report_result
end
report_that_this_element_does_not_exist(i) click to toggle source
#

report_that_this_element_does_not_exist

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 230
def report_that_this_element_does_not_exist(i)
  opnn; e 'The given element at `'+sfancy(i.to_s)+'` does not exist.'
  exit
end
reset() click to toggle source
#

reset

#
Calls superclass method ChemistryParadise::Base#reset
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 65
def reset
  super()
  # ======================================================================= #
  # === @commandline_arguments
  # ======================================================================= #
  @commandline_arguments = []
  # ======================================================================= #
  # === @show_more
  # ======================================================================= #
  @show_more = true
  # ======================================================================= #
  # === @show_the_steps
  #
  # If the following variable is set to true then we will display,
  # on the commandline, what is done when. This gives the user more
  # information about what is going on, and it may also help during
  # debugging this class/project.
  #
  # Note that @show_the_steps is a bit different to @show_more, so
  # these two variables are presently kept separate.
  # ======================================================================= #
  @show_the_steps = false
  # ======================================================================= #
  # === @individual_components
  # ======================================================================= #
  @individual_components = []
  # ======================================================================= #
  # === @output_string
  # ======================================================================= #
  @output_string = ''.dup
  # ======================================================================= #
  # === @report_result
  # ======================================================================= #
  @report_result = true
  set_input
end
result()
Alias for: result?
result?() click to toggle source
#

result?

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 245
def result?
  @result
end
Also aliased as: result, masse?
run() click to toggle source
#

run (run tag)

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 397
def run
  check_against_menu
  use_this_input = input?
  if use_this_input.to_s.size < 3
    unless is_included?
      report_that_this_element_does_not_exist(use_this_input)
    end
  end
  calculate_result
  consider_modifying_the_output_string
  report_result if report_result?
end
sanitize_input() click to toggle source
#

sanitize_input

This method will presently sanitize the input, by working on ₂ and on ₃, and replace these with the corresponding numbers. This may be useful depending on the chemical formula at hand, such as carbon dioxide (CO₂).

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 361
def sanitize_input
  @input.tr!('₂','2') if @input.include? '₂'
  @input.tr!('₃','3') if @input.include? '₃'
end
sanitize_input_for_element_names(array) click to toggle source
#

sanitize_input_for_element_names

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 136
def sanitize_input_for_element_names(array)
  result = []
  if array.any? {|entry| entry == entry.downcase }
    array.each {|entry|
      case entry
      when *ARRAY_HELP_OPTIONS
        show_help :then_exit
      end
      if entry == entry.downcase
        result[-1] = result[-1]+entry
      else
        result << entry
      end
    }
  else
    result = array
  end
  return result
end
set_input(i = DEFAULT_INPUT) click to toggle source
#

set_input

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 105
def set_input(i = DEFAULT_INPUT)
  if i.is_a? Array
    @commandline_arguments = i[1..-1] # Set the remaining as commandline arguments.
    i = i.first # And re-assign the first input.
  end
  i = DEFAULT_INPUT if i.nil?
  case i.to_s
  when 'do_not_report'
    @report_result = false
    i = nil
  end
  i = ARRAY_TEST_THESE_MOLECULES if i == :test_default_molecules
  i = i.to_s.dup
  # if i.include? ', '
  #   i = i.split(',').map(&:strip)
  # end
  # i = [i] unless i.is_a? Array
  @input = i
  sanitize_input
end
set_result(i) click to toggle source
#

set_result

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 186
def set_result(i)
  @result = i
end
show_help(i = :then_exit) click to toggle source
#

show_help

#
# File lib/chemistry_paradise/calculate_atomic_mass.rb, line 320
def show_help(i = :then_exit)
  opn; e 'Pass -SUM to show the individual sub-steps as well.'
  exit if i == :then_exit
end