Sha256: 401dbd99bfa49c2bb090ee40ffa304b3a421ef30b2b884ec2ffc8078ff07329c
Contents?: true
Size: 1.53 KB
Versions: 1
Compression:
Stored size: 1.53 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 @constituents = constituents @molecular_mass = molecular_mass end private def molecular_mass mass = 0 @constituents.each do |const| mass += const[:element].atomic_mass * const[:atom_count] end mass end def constituents constituents = [] get_const_atoms.each do |symbol, count| constituents << { 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.9 | lib/eulim/chemistry/compound.rb |