#include "TRuby.h"
#include "TROOT.h"
#include "TSystem.h"
#include "ruby.h"
ClassImp(TRuby)
extern VALUE cTObject;
bool TRuby::Initialize()
{
static int IsInitialized = 0;
if (!IsInitialized)
{
ruby_init();
IsInitialized = 1;
}
return true;
}
void TRuby::Exec(const char *cmd)
{
int state = 0;
TRuby::Initialize();
rb_eval_string_protect(cmd, &state);
if (state) rb_eval_string("puts $!");
}
TObject *TRuby::Eval(const char* expr)
{
TObject *res;
int state = 0;
TRuby::Initialize();
VALUE ret = rb_eval_string_protect(expr, &state);
if (state)
{
rb_eval_string("puts $!");
return (TObject*)(0);
}
if (NIL_P(ret)) return (TObject*)0;
VALUE ptr = rb_iv_get(ret, "__rr__");
if (!NIL_P(ptr))
{
Data_Get_Struct(rb_iv_get(ret, "__rr__"), TObject, res);
return res;
}
return (TObject*)0;
}
bool TRuby::Bind(TObject *obj, const char *label)
{
VALUE *v = ALLOC(VALUE);
*v = rb_class_new_instance (0, 0, cTObject);
rb_iv_set(*v, "__rr__", Data_Wrap_Struct (cTObject, 0, 0, obj));
rb_define_variable(label, v);
return true;
}
void TRuby::Prompt()
{
gSystem->Exec("irb");
}