RDF::TriG reader/writer

TriG reader/writer for RDF.rb .

Gem Version Build Status Coverage Status Dependency Status


This is a Ruby implementation of a TriG reader and writer for RDF.rb.


RDF::TriG parses TriG into statements or quads. It also serializes to TriG.

Install with gem install rdf-trig


Instantiate a reader from a local file:

repo = RDF::Repository.load("etc/doap.trig", :format => :trig)

Define @base and @prefix definitions, and use for serialization using :base_uri an :prefixes options.

Canonicalize and validate using :canonicalize and :validate options.

Write a repository to a file:

RDF::TriG::Writer.open("etc/test.trig") do |writer|
   writer << repo

Note that reading and writing of graphs is also possible, but as graphs have only a single context, it is not particularly interesting for TriG.


Full documentation available on Rubydoc.info.

Principle Classes

Implementation Notes

This version uses a hand-written parser using the Lexer from the [EBNF][] gem instead of a general [EBNF][] LL(1) parser for faster performance.

The reader uses the Turtle parser. The writer also is based on the Turtle writer.

The syntax is compatible with placing default triples within {}, but the writer does not use this for writing triples in the default graph.

There is a new :stream option to {RDF::TriG::Writer} which is more efficient for streaming large datasets.



The recommended installation method is via RubyGems. To install the latest official release of the RDF::TriG gem, do:

% [sudo] gem install rdf-trig

Mailing List




This is free and unencumbered public domain software. For more information, see http://unlicense.org/ or the accompanying {file:UNLICENSE} file.

A copy of the TriG EBNF and derived parser files are included in the repository, which are not covered under the UNLICENSE. These files are covered via the W3C Document License.