You are here

Ruby and ROOT

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.

Several examples are provided in the $ROOTSYS/tutorials/ruby directory. Here is a simple one:

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

Any Ruby command can be issued from CINT via TRuby:

root[] TRuby::Exec("require '/usr/local/lib/root/libRuby'"); /* put your path here*/
root[] TRuby::Exec("c1 = TBrowser.new");
root[] TRuby::Eval("c1.GetName");
root[] TRuby::Eval("puts c1.GetName");
/*Browser*/
root[] TCanvas *c2 = new TCanvas("ruby test", "test", 0, 0, 100, 100);
root[] TRuby::Bind(c2, "$c");
root[] TRuby::Eval("puts $c.GetTitle");
/*test*/