RDF::Turtle reader/writer

Turtle reader/writer for RDF.rb .

Gem Version Build Status Coverage Status Dependency Status


This is a Ruby implementation of a Turtle parser for RDF.rb.


RDF::Turtle parses Turtle and N-Triples into statements or triples. It also serializes to Turtle.

Install with gem install rdf-turtle


Instantiate a reader from a local file:

graph = RDF::Graph.load("etc/doap.ttl", format:  :ttl)

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 graph to a file:

RDF::Turtle::Writer.open("etc/test.ttl") do |writer|
   writer << graph


Full documentation available on Rubydoc.info

Principle Classes

Variations from the spec

In some cases, the specification is unclear on certain issues:

Freebase-specific Reader

There is a special reader useful for processing Freebase Dumps. To invoke this, add the freebase: true option to the {RDF::Turtle::Reader.new}, or use {RDF::Turtle::FreebaseReader} directly. As with {RDF::Turtle::Reader}, prefix definitions may be passed in using the :prefixes option to RDF::Turtle::FreebaseReader} using the standard mechanism defined for RDF::Reader.

The Freebase Dumps have a very normalized form, similar to N-Triples but with prefixes. They also have a large amount of garbage. This Reader is optimized for this format and will perform faster error recovery.

An example of reading Freebase dumps:

require "rdf/turtle"
fb = "../freebase/freebase-rdf-2013-03-03-00-00.ttl"
fb_prefixes = {
  ns:  "http://rdf.freebase.com/ns/",
  key:  "http://rdf.freebase.com/key/",
  owl:  "http://www.w3.org/2002/07/owl#>",
  rdfs:  "http://www.w3.org/2000/01/rdf-schema#",
  rdf:  "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
  xsd:  "http://www.w3.org/2001/XMLSchema#"
  freebase:  true,
  prefixes:  fb_prefixes) do |r|

  r.each_statement {|stmt| puts stmt.to_ntriples}

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 recommended installation method is via RubyGems. To install the latest official release of the RDF::Turtle gem, do:

% [sudo] gem install rdf-turtle

Mailing List



This repository uses Git Flow to mange development and release activity. All submissions must be on a feature branch based on the develop branch to ease staging and integration.


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 Turtle 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.