Some Ruby functions to manipulate graph files
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Build Status

The goal of this project is to provide some useful Ruby functions to manipulate graph files.

Note: some of the examples below are outdated, since before the 0.1.5 version, nodes & edges were represented as hashes, and now they are Node & Edge objects, respectively. However, the principles stay the sames.


The best way is to use the graphs gem:

gem install graphs

If you want to have the latest version, clone this repo, build the gem, and install it:

git clone git://
cd Graphs.rb
gem build graphs.gemspec
gem install ./graphs-*.gem # you may want to use sudo


To perform the tests, clone this repo, run bundle install, then rake (you need Ruby ≥1.9.x):

git clone git://
cd Graphs.rb
bundle install
bundle exec rake


The Graph class is a simple graph with nodes and edges. It provides three read-write attributes: nodes, edges, and attr (attributes of the graph, like author or description). It can be written in a file using Graph#write method.

Node and Edge are special classes which inherit from Hash. A graph object provide two important attributes:

  • nodes: A NodeArray object (Array-like)
  • edges: An EdgeArray object (Array-like, too)

For backward compatibility, you can create nodes and edges both with Node and Edge objects, and Hash ones, e.g.:

require 'graph'

nodes = [ {:label => 'foo'}, {:label => 'bar'} ]

g = nodes

nodes2 = { |n| n }
g2 = nodes2

g == g2 # true

You can perform some operations on graphes using the |, &, ^, + or - operators. See the documentation for more informations.


The library currently support JSON and GDF formats.

You can read from files using the ::load methods of each module:

require 'graph'
require 'graphs/gdf'
require 'graphs/json'

g1 = GDF::load('myGraph.gdf')
g2 = JSONGraph::load('myGraph.json')

You can also export a graph using the .write method. It guesses the format using the file extension.

require 'graph'
require 'graphs/gdf'
require 'graphs/json'

g =


Note to Gephi users who want to export in GDF: You can add the :gephi option to g.write(…) if you have big numbers. Graph#write method use BIGINT type for big numbers, but Gephi does not support it and parses it as a string field. So using the following:

g.write('new_trips.gdf', {:gephi=>true})

make sure that INT is used for all BIGINT fields.