SWIG question: how to return an array from C to Ruby ?

  • Thread starter Joachim (München)
  • Start date
J

Joachim (München)

I need to make some C functions available to Ruby. One of them sets an
array of doubles. I want to return this array to Ruby.

On the C side:

data_reader.c:
#include "hardwaredriver.h"
....
void read_data( double *data )
{
for( i=0; i<8; ++i )
data = .... // get values from hardware driver
}

On the Ruby side, I would like to obtain the following

irb> require "data_reader"
true
irb> arr = Data_reader.read_data
[ 0.0, 0.1, ... ] # arr now contains eight Floats


Question 1:
Do I understand correctly that there are basically two different ways
to proceed ?:
(a) wrapping the C code with SWIG,
(b) converting the C code to an explicit Ruby extension as
described in the Book: http://www.rubycentral.com/pickaxe/ext_ruby.html

IF the answer is yes, THEN
Question 2:
How do I return an array through C->SWIG-> Ruby ?

Web search gave me some hints on how to do it in the opposite
direction, using typemap(in); but it seems not trivial to invert
to typemap(out).

Thanks in advance, Joachim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top