Class: Alumina::Molecule
- Includes:
- HIN::Writer::Molecule
- Defined in:
- lib/alumina/molecule.rb
Overview
A molecule as represented by HIN data, consisting of multiple Atoms.
Instance Attribute Summary collapse
-
#id ⇒ Fixnum
The unique numerical identifier for this molecule.
-
#label ⇒ String?
The optional label given to this molecule.
Instance Method Summary collapse
-
#<<(atom) ⇒ Object
Adds an atom to this molecule.
-
#atom(ident) ⇒ Atom?
(also: #[])
Returns an atom for a given unique identifier.
-
#atoms ⇒ Array<Atom>
An array of atoms in this molecule.
-
#initialize(id, label = nil) ⇒ Molecule
constructor
Creates a new instance.
- #inspect ⇒ Object
-
#molecular_formula ⇒ String
for example, @C7H5N3O6@ for TNT.
Methods included from HIN::Writer::Molecule
Constructor Details
Instance Attribute Details
#id ⇒ Fixnum
Returns The unique numerical identifier for this molecule.
9 10 11 |
# File 'lib/alumina/molecule.rb', line 9 def id @id end |
#label ⇒ String?
Returns The optional label given to this molecule.
11 12 13 |
# File 'lib/alumina/molecule.rb', line 11 def label @label end |
Instance Method Details
#<<(atom) ⇒ Object
Adds an atom to this molecule. If there is already an atom in this molecule sharing this atom’s ID, it will be replaced by this atom.
31 32 33 |
# File 'lib/alumina/molecule.rb', line 31 def <<(atom) @atoms[atom.id] = atom end |
#atom(ident) ⇒ Atom? Also known as: []
Returns an atom for a given unique identifier.
atom was found.
47 48 49 |
# File 'lib/alumina/molecule.rb', line 47 def atom(ident) @atoms[ident] end |
#atoms ⇒ Array<Atom>
Returns An array of atoms in this molecule.
37 38 39 |
# File 'lib/alumina/molecule.rb', line 37 def atoms @atoms.values end |
#inspect ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/alumina/molecule.rb', line 62 def inspect if label then "#<Molecule ##{id} (#{label}): #{molecular_formula}>" else "#<Molecule ##{id}: #{molecular_formula}>" end end |
#molecular_formula ⇒ String
for example, @C7H5N3O6@ for TNT.
56 57 58 59 |
# File 'lib/alumina/molecule.rb', line 56 def molecular_formula counts = atoms.map(&:element).inject(Hash.new(0)) { |hsh, cur| hsh[cur] += 1 ; hsh } counts.keys.sort.reverse.map { |element| "#{element.symbol}#{num_for counts[element]}" }.join end |