How to Use the Ruby Interpreter ?

Ruby ROOT is a Ruby extension module that allows the user to interact with any ROOT class from the Ruby scripting language. The Ruby module resolves ROOT Classes and Methods at run-time using the CINT API, so there is no need for wrapping specific Classes to be used in Ruby. The Ruby module, also, contains a TRuby class to execute Ruby statements via CINT and export C++ Objects to Ruby objects, interactively.
To learn more about the Ruby-Root interface see the Diploma Thesis of Elias Athanasopoulos (elathan@ics.forth.gr).

Building and installing the Ruby module

The Ruby extension module is not built by default when building ROOT from sources. The user should follow the standard installation instructions and enable the build of the Ruby module. Ruby version >= 1.8 is required.

./configure <arch> --enable-ruby --enable-explicitlink \
[--with-ruby-incdir=<dir>] [--with-ruby-libdir=<dir>]
gmake

If you do not specify the inc and lib directories configure will use Ruby to grab the directories where Ruby's headers and library are located.

A library called libRuby.so [libRuby.dll] will be created in the $ROOTSYS/lib [$ROOTSYS/bin].

Setting up the environment

To work with the Ruby module, the LD_LIBRARY_PATH [PATH] and RUBYLIB need to be set in addition to the standard ROOTSYS.

For Unix platforms

export LD_LIBRARY_PATH=$ROOTSYS/lib:$LD_LIBRARY_PATH
export RUBYLIB=$ROOTSYS/lib:$RUBYLIB

for Windows

set PATH=%ROOTSYS%/bin;%PATH%
set RUBYLIB=%ROOTSYS%/bin;%RUBYLIB%

Running ROOT scripts from Ruby

The user should make sure that the ruby command is the one of the installation that has been used to build the Ruby extension module. If the RUBYLIB enviroment variable is set correctly, the user can execute a Ruby script with ROOT functionality in the following way:

ruby -rlibRuby foo.rb
Another way is to start the Ruby script with the Ruby require command:
require 'libRuby'
An example is as follows:

require 'libRuby'
 
gROOT.Reset
c1 = TCanvas.new('c1', 'Example with Formula', 200, 10, 700, 500)
 
#
# Create a one dimensional function and draw it
#
fun1 = TF1.new('fun1', 'abs(sin(x)/x)', 0, 10)
c1.SetGridx
c1.SetGridy
fun1.Draw
c1.Update

The user can find a number of examples in the $ROOTSYS/tutorials. To run them you need to execute the command:

cd $ROOTSYS/tutorials
ruby demo.rb

Invoking the Ruby module from ROOT/CINT interpreter

A ROOT user can run any Ruby command and eventually to run IRB, the Interactive Ruby Shell. The commands to execute are:

root [0] gSystem&gt;Load("libRuby");
root [1] TRuby::Exec("require '/usr/local/lib/root/libRuby'");
root [2] TRuby::Exec("c1 = TBrowser.new");
root [3] TRuby::Eval("c1.GetName");
root [4] TRuby::Eval("puts c1.GetName");
Browser
root [5] TCanvas *c2 = new TCanvas("ruby test", "test", 10, 10, 100, 100);
root [6] TRuby::Bind(c2, "$c");
root [7] TRuby::Eval("puts $c.GetTitle");
test
root [8] TRuby::Prompt();
root [9] TRuby::Prompt();
irb(main):001:0> print 1
1=> nil
irb(main):002:0>

Notice that whenever you bind a ROOT Object in the Ruby side, you need to use a global Ruby variable, that is a variable with a leading "$".