module ChemistryParadise::Shared

Constants

ARRAY_TEST_THESE_MOLECULES
#

ARRAY_TEST_THESE_MOLECULES

Test these molecules. Default values.

#

Public Class Methods

convert_parens(i) click to toggle source
#

ChemistryParadise::Shared.convert_parens

This method will properly convert the () parens that can be found in a chemical formula.

For instance:

Al2(SO4)3

Is effectively the same such as:

Al2SO4SO4SO4
#
# File lib/chemistry_paradise/shared.rb, line 79
def self.convert_parens(i)
  if i.is_a? Array
    i.each {|entry| convert_parens(entry) }
  else
    if i.include? '('
      n_parens = i.count('(')
      if n_parens > 1 # Ok, this may be a really complex formula, like:
                      #   Fe(OH)3 + H2SO4 -> Fe2(SO4)3 + H2O
        splitted = i.split(' ')
        splitted = splitted.map {|entry| convert_parens(entry) } # For now we remove some things.
        return splitted.join(' ')
      else
        regex = /\((.+)\)(\d)/
        match = i.match(regex)
        i = i[0, i.index('(')]
        i << match[1] * match[2].to_i # Which group * n repetition
      end
    end
    return i
  end
end
periodic_table?() click to toggle source
#

ChemistryParadise::Shared.periodic_table?

#
# File lib/chemistry_paradise/shared.rb, line 111
def self.periodic_table?
  ::ChemistryParadise.periodic_table?
end

Public Instance Methods

convert_parens(i) click to toggle source
#

convert_parens

#
# File lib/chemistry_paradise/shared.rb, line 151
def convert_parens(i)
  ChemistryParadise::Shared.convert_parens(i)
end
Also aliased as: parse
is_number?(i) click to toggle source
#

is_number?

This method will return true if is a number, else false. For this it uses the .to_i method trick, which returns 0 for non-numbers.

#
# File lib/chemistry_paradise/shared.rb, line 52
def is_number?(i)
  result = (i.to_i.to_s == i)
  return result
end
parse(i)
Alias for: convert_parens
periodic_table?() click to toggle source
#

periodic_table?

#
# File lib/chemistry_paradise/shared.rb, line 104
def periodic_table?
  return Shared.periodic_table?
end
return_range_for_this_period(this_period = 1) click to toggle source
#

return_range_for_this_period

This method will tell us the legal range of elements for any given period.

The formula would be:

2+4n

For instance:

period 1 will return (1..2).
period 2 will return (3..8).
#
# File lib/chemistry_paradise/shared.rb, line 129
def return_range_for_this_period(this_period = 1)
  case this_period
  when 1
    (1..2)
  when 2
    (3..10)
  when 3
    (11..18)
  when 4
    (19..36)
  when 5
    (37..54)
  when 6
    (55..86)
  when 7
    (87..118)
  end
end
sqr(of)
Alias for: square
square(of) click to toggle source
#

square

Die Wurzel aus 2 ist 1.414

chem; square 2; sqr 3; sqr 4; sqr 5; sqr 6; sqr 7; sqr 8; sqr 9
#
# File lib/chemistry_paradise/shared.rb, line 63
def square(of)
  Math.sqrt of
end
Also aliased as: sqr