class Mspire::MolecularFormula

Constants

VERSION

Attributes

charge[RW]

integer desribing the charge state mass calculations will add/remove electron mass from this

Public Class Methods

new(hash={}, charge=0) click to toggle source

Takes a hash and an optional Integer expressing the charge

{H: 22, C: 12, N: 1, O: 3, S: 2}  # case and string/sym doesn't matter
# File lib/mspire/molecular_formula.rb, line 10
def initialize(hash={}, charge=0)
  @charge = charge
  self.merge!(hash)
end

Public Instance Methods

==(other) click to toggle source
# File lib/mspire/molecular_formula.rb, line 41
def ==(other)
  old_equal(other) && self.charge == other.charge
end
Also aliased as: old_equal
inspect() click to toggle source
# File lib/mspire/molecular_formula.rb, line 30
def inspect
  "{MolecularFormula #{super[1...-1]}, @charge=#{self.charge}}"
end
old_equal(other)
Alias for: ==
to_h() click to toggle source

returns a hash (note: does not pass along charge info!)

# File lib/mspire/molecular_formula.rb, line 35
def to_h
  Hash[ self ]
end
to_s(include_charge_if_nonzero=true, alphabetize=true) click to toggle source
# File lib/mspire/molecular_formula.rb, line 15
def to_s(include_charge_if_nonzero=true, alphabetize=true)
  h = alphabetize ? self.sort : self
  st = ''
  h.each do |k,v|
    if v > 0
      st << k.to_s.capitalize
      st << v.to_s if v > 1
    end
  end
  if include_charge_if_nonzero
    st << "#{charge > 0 ? '+' : '-'}#{charge.abs if charge.abs > 1}" unless charge.zero?
  end
  st
end