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
new( optional_input = ARGV, run_already = true )
click to toggle source
Public Instance Methods
atomgewichte?()
click to toggle source
bold_red(i)
click to toggle source
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
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_the_steps()
click to toggle source
gather_individual_components(name, weight)
click to toggle source
input?()
click to toggle source
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?
nice_result()
click to toggle source
opnn()
click to toggle source
report_result()
click to toggle source
report_result?()
click to toggle source
report_that_this_element_does_not_exist(i)
click to toggle source
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?()
click to toggle source
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