Sha256: 5c277d70f7ea4bb93d9fbc28e821699e781c23413fea5100367d8c49af7743e7
Contents?: true
Size: 1.63 KB
Versions: 1
Compression:
Stored size: 1.63 KB
Contents
module Eulim module Chemistry # This class has functionality for compounds # Ex: constituent elements, molecular mass, etc class Compound COMPOUND_REGEXP = /[A-Z][a-z]{0,2}\d*|\((?:[^()]*(?:\(.*\))?[^()]*)+\)\d*/ attr_accessor :molecular_mass, :constituents, :formula def initialize(arg) @formula = arg build_constituents calculate_molecular_mass end private def calculate_molecular_mass @molecular_mass = 0 @constituents.each do |_symbol, info| @molecular_mass += info[:element].atomic_mass * info[:atom_count] end @molecular_mass end def build_constituents @constituents = {} get_const_atoms.each do |symbol, count| @constituents[symbol] = { element: Element.get_by_symbol(symbol), atom_count: count } end @constituents end def get_const_atoms(formula = @formula, r = {}) formula.scan(COMPOUND_REGEXP).each do |const| multipler = get_multipler const if const[0] != '(' && multipler.zero? r[const] = r[const] ? r[const] + 1 : 1 else (multipler.zero? ? 1 : multipler).times do sub_const = const.match(/^\(?(.*?)\)?($|\d*$)/).to_a get_const_atoms sub_const[const == sub_const.first ? 1 : 0], r end end end r end def get_multipler(const) multipler = const.match(/\d*$/).to_a.first.to_i multipler end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
eulim-0.0.10 | lib/eulim/chemistry/compound.rb |