Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > SWIG question: how to return an array from C to Ruby ?

Reply
Thread Tools

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

 
 
Joachim (München)
Guest
Posts: n/a
 
      08-09-2007
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[i] = .... // 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to access an Array and a struct within a struct wrapped by Swig ? emmanuel.rivoire@gmail.com Python 3 08-18-2008 03:30 AM
void * C array to a Numpy array using Swig Krish Python 5 01-13-2006 01:01 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Questions about SWIG, Ruby/DL and embedding calls to ruby withinC++ Code Charles Comstock Ruby 1 06-25-2004 09:49 PM



Advertisments