From: Lyle Johnson Date: 2003-02-19T03:08:52+09:00 Subject: Re: Question about SWIGing a C++ array for Ruby Glenn M. Lewis wrote: > I think I solved this one with a hack... in the CSIChart.h file, > I put: > ================= > #ifndef SWIG > CSITick* ticks; > #endif > ================= > and then I added to the RubyStock.i file: > ======================================================================== > %include "CSIChart.h" > %extend CSIChart { > CSITick* ticks(int index) { > while (index < 0) index += self->numticks; // wrap-around > if (index >= self->numticks) { > printf("ERROR: index (%d) out of range for ticks array: 0...%d\n", > index, self->numticks); > return 0; > } > return &self->ticks[index]; > } > }; > ======================================================================== > This works, but is there a way to turn this into a Ruby Array > so that 'ticks' could be accessed with the '[]' notation? I think I would change the %extend-ed version of ticks() to look something like this instead: %extend CSIChart { VALUE ticks() const { VALUE ary = rb_ary_new(); for (long i = 0; i < self->numticks; i++) { VALUE tickObj = SWIG_NewPointerObj(self->ticks[i], SWIGTYPE_p_CSITick, 0); rb_ary_push(ary, tickObj); } return ary; } }; Hope this helps, Lyle