Ruby API
Parsing
Section titled “Parsing”formula = AsciiChem.parse("2H_2 + O_2 -> 2H_2O")# => #<AsciiChem::Model::Formula>
# Every input grammar lands in the same model:AsciiChem.parse_smiles("CC(=O)OC1=CC=CC=C1C(=O)O") # aspirinAsciiChem.parse_molfile(molfile_text) # V2000, coordinates preservedAsciiChem::Cml.parse(cml_xml) # CML in, AsciiChem outModel shortcuts
Section titled “Model shortcuts”Every Model::Node provides output shortcuts:
formula.to_text # canonical AsciiChem textformula.to_mathml # MathML XMLformula.to_html # inline HTMLformula.to_latex # LaTeX (mhchem \ce{})formula.to_svg # linear SVG wrapperformula.to_structural_svg # 2D structural SVG (elkrb for molecules)formula.to_cml # CML XML stringformula.to_model_json # asciichem-model v1 wire JSONformula.to_smiles # canonical SMILES (molecules)mol.to_molfile # V2000 molfile (molecules)Molecule convenience methods (v0.12+)
Section titled “Molecule convenience methods (v0.12+)”mol = AsciiChem.parse("C_6H_12O_6").nodes.first
mol.atom_count # => 24 (total atoms with subscripts/mults)mol.hill_formula # => "C6H12O6"mol.formula_weight # => 180.156 (IUPAC atomic masses)Atom value objects (v0.17+)
Section titled “Atom value objects (v0.17+)”atom = AsciiChem.parse("C@(1,2,3)").nodes.first.nodes.first
atom.cartesian # => #<Point3 x=1, y=2, z=3>atom.cartesian.to_a # => [1, 2, 3]atom.cartesian.magnitude # => 3.74
atom.fractional # => nil (no fractional coords set)CML pipeline
Section titled “CML pipeline”# AsciiChem → CMLxml = AsciiChem::Cml.from_asciichem(formula)
# CML → AsciiChemformula = AsciiChem::Cml.parse(xml)Linter
Section titled “Linter”diagnostics = AsciiChem::Linter.run(formula)diagnostics.any? { |d| d.severity == :error } # => false if cleanInChI identity
Section titled “InChI identity”AsciiChem::Inchi.engine = AsciiChem::Inchi::BinaryEngine.new # IUPAC inchi-1 binary# or: AsciiChem::Inchi::NativeEngine.new # chemomer Rust engine (CHEMOMER_LIB)AsciiChem::Inchi.identity_for(molecule) # => Identity(inchi: ..., inchikey: ...)Without an engine, derivation raises EngineMissingError with
install guidance. Engine choices, the RInChI reaction path, and what
crosses into the engine are on Local identity.
Formatter registry
Section titled “Formatter registry”AsciiChem::Formatter[:mathml] # => AsciiChem::Formatter::MathmlAsciiChem::Formatter.render(:html, formula)Resolution (v0.25+)
Section titled “Resolution (v0.25+)”Identifiers become substance records with per-identifier provenance. Every shipped source — PubChem, OPSIN, and CAS Common Chemistry — is registered and on by default (Common Chemistry is CC BY-NC 4.0, default-on per the maintainer decision of 2026-11-11); limit the set explicitly when your use requires it:
# one record per answering source (nil dropped); first wins for #resolveAsciiChem::Resolver.resolve(value: "50-78-2", convention: "cas")AsciiChem::Resolver.resolve_all(value: "50-78-2", convention: "cas")# => [#<AsciiChem::Resolver::Substance ...>, ...] (PubChem + Common Chemistry)
# limit the set (NC-free resolution)AsciiChem::Resolver.resolve_all(value: "50-78-2", convention: "cas", sources: [:pubchem, :opsin])
# cross-check mode: raises Resolver::Conflict when InChIKeys disagreeAsciiChem::Resolver.resolve!(value: "50-78-2", convention: "cas")Source conventions: PubChem answers name, cas, pubchem-cid,
inchikey, inchi, smiles, and canonical-smiles; OPSIN answers
iupac-name; Common Chemistry answers cas. Results cache in your
user cache directory (7-day TTL; refresh: true bypasses).
Citation (v0.25+)
Section titled “Citation (v0.25+)”Resolution produces a substance record with provenance; the citation
builder turns that record into a Relaton dataset-type bibitem — one
per (substance, source):
substance = AsciiChem::Resolver.resolve(value: "50-78-2", convention: "cas")AsciiChem::Citation.to_xml(substance) # <bibitem type="dataset"> XML
# The @cite annotation, from the parse tree:molecule = AsciiChem.parse('H_2O @name("aspirin") @cite("pubchem")').nodes.firstAsciiChem::Citation.for_molecule(molecule)# => [["pubchem", #<Relaton::BibliographicItem ...>]]The bibitem shape, per-source fields, and the licensing story are on Citing substances.