Skip to content
This project has relocated to the Ruby-RDF organization:
Pull request Compare This branch is 677 commits behind ruby-rdf:master.
Latest commit 395f1d1 @gkellogg gkellogg Merge pull request #12 from dwbutler/master
Try running the specs in Ruby 2.0.0 and head
Failed to load latest commit information.
bin Fixed 1.8.7 compatibility issues in rdf/cli and bin/rdf.
etc Corrected my email address in the AUTHORS file and DOAP data.
lib Merge pull request #8 from jcoyne/w3c_ma_ont
.gemspec Yard documentation updates.
.gitignore Remove Gemfile.lock
.travis.yml Try running the specs in Ruby 2.0.0 and head
Gemfile Set default encoding in spec_helper
README Changed README format from RDoc to Markdown. Yard documentation updates.
Rakefile Added .travis.yml.
UNLICENSE Unlicensed the project into the public domain.
VERSION Yard documentation updates.
rdf.gemspec Renamed the gemspec back to the proper name while preserving Bundler …

RDF.rb: Linked Data for Ruby

Build Status

This is a pure-Ruby library for working with Resource Description Framework (RDF) data.


  • 100% pure Ruby with minimal dependencies and no bloat.
  • 100% free and unencumbered public domain software.
  • Provides a clean, well-designed RDF object model and related APIs.
  • Supports parsing and serializing N-Triples out of the box, with more serialization format support available through add-on plugins.
  • Includes in-memory graph and repository implementations, with more storage adapter support available through add-on plugins.
  • Implements basic graph pattern (BGP) query evaluation.
  • Plays nice with others: entirely contained in the RDF module, and does not modify any of Ruby's core classes or standard library.
  • Based entirely on Ruby's autoloading, meaning that you can generally make use of any one part of the library without needing to load up the rest.
  • Compatible with Ruby 1.8.7+, Ruby 1.9.x, and JRuby 1.4/1.5.
  • Compatible with older Ruby versions with the help of the Backports gem.
  • Performs auto-detection of input to select appropriate Reader class if one cannot be determined from file characteristics.


Command Line

When installed, RDF.rb includes a rdf shell script which acts as a wrapper to perform a number of different operations on RDF files using available readers and writers.

  • serialize: Parse an RDF input and re-serializing to N-Triples or another available format using --output-format option.
  • count: Parse and RDF input and count the number of statements.
  • subjects: Returns unique subjects from parsed input.
  • objects: Returns unique objects from parsed input.
  • predicates: Returns unique objects from parsed input.


require 'rdf'
include RDF

Writing RDF data using the N-Triples format

require 'rdf/ntriples'
graph = << [:hello, RDF::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'

graph = RDF::Graph.load("", :format => :nquads)

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

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

Reader/Writer implementations may override {RDF::Format.detect}, which takes a small sample if input and return a boolean indicating if it matches that specific format. In the case that a format cannot be detected from filename or other options, or that more than one format is identified, {RDF::Format.for} will query each loaded format by invoking it's detect method, and the first successful match will be used to read the input.

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'"hello.nq", :format => :nquads) do |writer|
  writer << do |repo|
    repo <<, RDF::DC.title, "Hello, world!", :context => RDF::URI("context"))

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


Querying RDF data using basic graph patterns (BGPs)

require 'rdf/ntriples'

graph = RDF::Graph.load("")
query ={
  :person => {
    RDF.type  => FOAF.Person, => :name,
    FOAF.mbox => :email,

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

A separate SPARQL gem builds on basic BGP support to provide full support for SPARQL 1.0 queries.

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("")


RDF Object Model

  • {RDF::Value}
    • {RDF::Term}
      • {RDF::Literal}
        • {RDF::Literal::Boolean}
        • {RDF::Literal::Date}
        • {RDF::Literal::DateTime}
        • {RDF::Literal::Decimal}
        • {RDF::Literal::Double}
        • {RDF::Literal::Integer}
        • {RDF::Literal::Time}
        • RDF::XSD (plugin)
      • {RDF::Resource}
        • {RDF::List}
        • {RDF::Node}
        • {RDF::URI}
        • {RDF::Graph}
    • {RDF::Statement}

RDF Serialization

  • {RDF::Format}
  • {RDF::Reader}
  • {RDF::Writer}

RDF Serialization Formats

The following is a partial list of RDF formats implemented either natively, or through the inclusion of other gems:

The meta-gem LinkedData includes many of these gems.

RDF Storage

RDF Querying

  • {RDF::Query}
    • {RDF::Query::Pattern}
    • {RDF::Query::Solution}
    • {RDF::Query::Solutions}
    • {RDF::Query::Variable}
  • SPARQL (plugin)

RDF Vocabularies

  • {RDF} - Resource Description Framework (RDF)
  • {RDF::CC} - Creative Commons (CC)
  • {RDF::CERT} - W3 Authentication Certificate (CERT)
  • {RDF::DC} - Dublin Core (DC)
  • {RDF::DC11} - Dublin Core 1.1 (DC11) deprecated
  • {RDF::DOAP} - Description of a Project (DOAP)
  • {RDF::EXIF} - Exchangeable Image File Format (EXIF)
  • {RDF::FOAF} - Friend of a Friend (FOAF)
  • {RDF::GEO} - WGS84 Geo Positioning (GEO)
  • {RDF::HTTP} - Hypertext Transfer Protocol (HTTP)
  • {RDF::OWL} - Web Ontology Language (OWL)
  • {RDF::RDFS} - RDF Schema (RDFS)
  • {RDF::RSA} - W3 RSA Keys (RSA)
  • {RDF::RSS} - RDF Site Summary (RSS)
  • {RDF::SIOC} - Semantically-Interlinked Online Communities (SIOC)
  • {RDF::SKOS} - Simple Knowledge Organization System (SKOS)
  • {RDF::WOT} - Web of Trust (WOT)
  • {RDF::XHTML} - Extensible HyperText Markup Language (XHTML)
  • {RDF::XSD} - XML Schema (XSD)



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

% [sudo] gem install rdf             # Ruby 1.8.7+ or 1.9.x
% [sudo] gem install backports rdf   # Ruby 1.8.1+


To get a local working copy of the development repository, do:

% git clone git://

Alternatively, download the latest development version as a tarball as follows:

% wget


Mailing List




  • Do your best to adhere to the existing coding conventions and idioms.
  • Don't use hard tabs, and don't leave trailing whitespace on any line.
  • Do document every method you add using YARD annotations. Read the tutorial or just look at the existing code for examples.
  • Don't touch the .gemspec or VERSION files. If you need to change them, do so on your private branch only.
  • Do feel free to add yourself to the CREDITS file and the corresponding list in the the README. Alphabetical order applies.
  • Don't touch the AUTHORS file. If your contributions are significant enough, be assured we will eventually add you in there.
  • Do note that in order for us to merge any non-trivial changes (as a rule of thumb, additions larger than about 15 lines of code), we need an explicit public domain dedication on record from you.


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

Something went wrong with that request. Please try again.