Re: a few ruby-root questions

From: Mike Williams <williams_at_ernest.phys.cmu.edu>
Date: Wed, 23 Aug 2006 09:30:23 -0400 (EDT)

hi,

thanks for the response...let me clarify my question.

it's realy nice the way you can have a function like this:

TList* SomeClass::get_histos();

then on the ruby side do:

some_class.get_histos.each{|h| ... }

so by a 'good way' of doing an array of strings, i meant something like this on the ruby side:

some_class.get_strs.each{|str| ... }

where the SomeClass::get_strs fills an array of some kind with c/c++ strings or char*'s that is automatically converted to a ruby Array of ruby String's.

so my question was, is there a way to do this?

my question about returning a VALUE object was refering to being able to do this by hand like:

VALUE SomeClass::get_strs(){

  VALUE str_ary = rb_ary_new();
  rb_ary_push(str_ary,rb_str_new2("hello"));
  rb_ary_push(str_ary,rb_str_new2("world!"));

  return str_ary;
}

but returning a VALUE object doesn't work (at least it didn't work for me)...i can only set the ruby variable by name by doing something like:

void SomeClass::get_strs(const char *__rb_var){

  VALUE str_ary = rb_ary_new();
  rb_ary_push(str_ary,rb_str_new2("hello"));
  rb_ary_push(str_ary,rb_str_new2("world!"));

  rb_gv_set(__rb_var,str_ary);
}

this works, but it's not the best interface. but if there's another way of doing this w/o using anything in ruby.h that'd be nice.

thanks alot.

mike williams

> > >2) is there a good way to deal with arrays of strings? it's nice how
> > >TArray's/TList's are converted to ruby arrays...but i can't store string
> > >or char* in these since anything in a TList must derive from TObject.
>
> Hm... Do you have anything in mind (refering to 'good way')?
>
> > >3) knowing this would answer #2...is there a way to simply return the
> > >VALUE object created in pure C code? i tried to include ruby.h when
> > >generating a dictionary and it gave lots of errors. what i can do is
> > >define a member method like:
> >
> > >void foo(const char *__var);
> > >
> > >then include the ruby.h file in the classes .C file (keeping it away from
> > >cint) then use the rb_ary_new etc...to fill an array with
> > >strings,floats,etc. and then use rb_gv_set to set a variable named __var
> > >on the ruby side to be the array. this works, but isn't the way i want to
> > >have to call things. if i knew how to simply return the VALUE object, then
> > >i could just fill the arrays myself.
> > >
> > >just to note, since i couldn't include the ruby.h file without getting
> > >lots of errors...i tried simply copying the VALUE typedef (which is
> > >unsigned long) into my classes .h file. this compiled and ran, however
> > >the VALUE ruby array ended up as just an integer (bignum) on the ruby
> > >side...so it was just an unsigned long.
>
> I'm not sure I'm getting you here. Every Object has a __rr__ method, which is
> the actual VALUE object and it actually encapsulates the pointer in the C
> side. See the drr_as() function in drr.cxx.
>
> Feel free to elaborate in this, if I am totally out of the question.
>
> Regards,
>
Received on Wed Aug 23 2006 - 15:30:40 MEST

This archive was generated by hypermail 2.2.0 : Mon Jan 01 2007 - 16:32:00 MET