From: "Glenn M. Lewis" Date: 2003-02-19T02:38:02+09:00 Subject: Re: Question about SWIGing a C++ array for Ruby 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? Thanks! -- Glenn Lewis Glenn M. Lewis wrote: > I've got these C++ classes: > > class CSITick { > public: > unsigned long date; > float open; > float high; > float low; > float close; > long volume; > }; > class CSIChart { > public: > CSITick* getTick(long date); // CAN return NULL!!! > CSITick* ticks; > long numticks; > //... > }; > > > where 'ticks' is an array of CSITick's, created with: > ticks = new CSITick[numticks]; > > I can't seem to figure out how to make SWIG access this > 'ticks' pointer as if it were an Array in Ruby, with length > equal to numticks. I would like it to be read-only as well. > > The '%typemap' SWIG directive doesn't seem right, as > this is the only place where I want to treat a CSITick* as an > Array... otherwise, I would like a CSITick* to just be a reference > to an instance of CSITick. Am I making any sense? > > I'm using SWIG-1.3.17 and ruby 1.6.8 (2002-12-24) [i586-mswin32] > on Windows XP. > > Thanks! > -- Glenn Lewis