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

Discussion in 'Ruby' started by Joachim (München), Aug 9, 2007.

  1. 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
     
    Joachim (München), Aug 9, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Krish
    Replies:
    5
    Views:
    732
    Travis E. Oliphant
    Jan 13, 2006
  2. Greenhorn
    Replies:
    15
    Views:
    839
    Keith Thompson
    Mar 6, 2005
  3. Charles Comstock
    Replies:
    1
    Views:
    122
    Lyle Johnson
    Jun 25, 2004
  4. Daniel Berger

    Swig, Ruby, C++ question

    Daniel Berger, Dec 4, 2006, in forum: Ruby
    Replies:
    3
    Views:
    96
    Jan Svitok
    Dec 4, 2006
  5. Tammo Tjarks
    Replies:
    0
    Views:
    101
    Tammo Tjarks
    Feb 7, 2007
Loading...

Share This Page