ruby-graphviz を使う

GraphVizを,rubyからあつかいませう.

sudo port install graphviz

sudo gem install ruby-graphviz --remote

  • サンプルプログラムを動かしてみる
    • 以下をsample01.rb として保存し(もともとruby-graphvizに入っているサンプルプログラムを gems用にrequire部分を変更したもの)
#!/usr/bin/ruby

require "rubygems"
gem "ruby-graphviz"
require "graphviz"


g = nil
if ARGV[0]
  g = GraphViz::new( "G", :path => ARGV[0] )
else
  g = GraphViz::new( "G" )
end

main        = g.add_node( "main" )
parse       = g.add_node( "parse" )
execute     = g.add_node( "execute" )
init        = g.add_node( "init" )
cleanup     = g.add_node( "cleanup" )
make_string = g.add_node( "make_string" )
printf      = g.add_node( "printf" )
compare     = g.add_node( "compare" )

g.add_edge( main, parse )
g.add_edge( parse, execute )
g.add_edge( main, init )
g.add_edge( main, cleanup )
g.add_edge( execute, make_string )
g.add_edge( execute, printf )
g.add_edge( init, make_string )
g.add_edge( main, printf )
g.add_edge( execute, compare )

g.output( :output => "png" )

% ruby sample01.rb > sample01.png

    • こんな感じ