Linked Data for Ruby

This is the home of Ruby RDF. This project collects numerous gems supporting Linked Data and Semantic Web programming in Ruby.

The primary gem is RDF.rb, which contains the core algorithms and classes used for doing basic programming of [RDF][], including support for Repositories, Graphs, Statements, URIs, Literals, and BNodes.

The Ruby RDF account also collects numerous gems used for reading and writing different RDF formats. At present, this includes the following:

In addition to basic Query mechanisms

Web infrastructure

There are also storage adaptors for popular Triple-stores, Graph-stores SQL, and other NOSQL stores.

There is also a LinkedData gem, which combines a core set of these together.


require 'linkeddata'

Writing RDF data using the N-Triples format

require 'rdf/ntriples'
graph = << [:hello, RDF::Vocab::DC.title, "Hello, world!"]

or"hello.nt") { |writer| writer << graph }

Reading RDF data in the N-Triples format

require 'rdf/ntriples'
graph = RDF::Graph.load("")

or"") do |reader|
  reader.each_statement do |statement|
    puts statement.inspect

Reading RDF data in other formats

{} and {RDF::Repository.load} use a number of mechanisms to determine the appropriate reader to use when loading a file. The specific format to use can be forced using, e.g. :format => :ntriples option where the specific format symbol is determined by the available readers. Both also use MimeType or file extension, where available.

require 'linkeddata'

repo = RDF::Repository.load("")

A specific sub-type of Reader can also be invoked directly:

require 'rdf/turtle'"") do |reader|
  reader.each_statement do |statement|
    puts statement.inspect

Writing RDF data using other formats, RDF::Enumerable#dump, RDF::Writer.dump take similar options to to determine the appropriate writer to use.

require 'linkeddata'"doap.nt") do |writer|
  writer <<"")

A specific sub-type of Writer can also be invoked directly:

require 'rdf/nquads'


Querying RDF data using basic graph patterns (BGPs)

require 'rdf/nquads'

repo = RDF::Repository.load("")
query ={
  :person => {
    RDF.type  => RDF::Vocab::FOAF.Person, => :name,
    RDF::Vocab::FOAF.mbox => :email,

query.execute(repo).each do |solution|
  puts "name=#{} email=#{}"

A separate [SPARQL][SPARQL doc] gem builds on basic BGP support to provide full support for SPARQL 1.1 queries and updates.

Using pre-defined RDF vocabularies

DC.title      #=> RDF::URI("")
FOAF.knows    #=> RDF::URI("")
RDF.type      #=> RDF::URI("")
RDFS.seeAlso  #=> RDF::URI("")
RSS.title     #=> RDF::URI("")
OWL.sameAs    #=> RDF::URI("")
XSD.dateTime  #=> RDF::URI("")

Using ad-hoc RDF vocabularies

foaf ="")
foaf.knows    #=> RDF::URI("")
foaf[:name]   #=> RDF::URI("")
foaf['mbox']  #=> RDF::URI("")

Presentations on RDF.rb and related technologies