Professional Web Applications Themes

convert a C/C++ array to a Ruby array - Ruby

Hello! While writting a Ruby extension, consider the prototype: double * foo(void); I would like, optimally, to return a Ruby array with floats, but the problem is that I don't know the length of the C array of doubles which foo() returns. Is there a way to accomplish such a thing? If not, how should the Right (TM) Ruby extension treat such a prototype? I am compiling with g++, so I can use all the functionality C++ provides. Regards, Elias...

  1. #1

    Default convert a C/C++ array to a Ruby array

    Hello!

    While writting a Ruby extension, consider the prototype:

    double * foo(void);

    I would like, optimally, to return a Ruby array with floats, but the problem is that I don't know
    the length of the C array of doubles which foo() returns.

    Is there a way to accomplish such a thing? If not, how should the Right (TM) Ruby
    extension treat such a prototype?

    I am compiling with g++, so I can use all the functionality C++ provides.

    Regards,
    Elias


    elathan@phys.uoa.gr Guest

  2. #2

    Default Re: convert a C/C++ array to a Ruby array

    Hi,

    At Thu, 8 Jan 2004 18:03:15 +0900,
    uoa.gr wrote: 

    How do you use it in C at all?

    --
    Nobu Nakada


    nobu.nokada@softhome.net Guest

  3. #3

    Default Re: convert a C/C++ array to a Ruby array

    Quoting net: 
    > problem is that I don't know 
    >
    > How do you use it in C at all?[/ref]

    Actually it is part of the functionallity a C++ Class embeds:

    Foo::Foo(int n, double *f);
    double * Foo::Get();

    Okay, I can grab 'n' from the constructor and use it as the length
    of the Ruby/C array, but I wanted to make sure that there is no
    general way to procceed without messing with the details of the
    C/C++ code.

    Regards,
    Elias


    elathan@phys.uoa.gr Guest

  4. #4

    Default Re: convert a C/C++ array to a Ruby array

    Hi,

    At Thu, 8 Jan 2004 18:46:21 +0900,
    uoa.gr wrote: 

    If you can get the length from Foo, you can use it.

    What do you really intend, pseudo array?

    --
    Nobu Nakada


    nobu.nokada@softhome.net Guest

  5. #5

    Default Re: convert a C/C++ array to a Ruby array

    Quoting net: 
    >
    > If you can get the length from Foo, you can use it.[/ref]

    Yes I know. The problem is that I am creating Ruby bindings for a large
    framework (more than 50 C++ classes) and the bindings code is automagically
    generated by reading the C++ prototypes, using a handmade Ruby
    pr/compiler.

    The generated code is quite readable and can be altered by a human, but
    I want to eliminate as much as I can the human intervation, so as the majority of the
    C++ prototypes to be compiled in the Ruby API without human editing.
     

    If I was doing this by myself, I would create a Ruby array of length Foo::n and
    populate it with the double(s) Foo::Get() returns.

    But this requires some magick, i.e. to understand further the functionality of Class Foo;
    not just read the prototypes and translate them using an application. So, I asked
    here if someone knows a better way...

    I checked SWIG and it returns a special SWIG object in that case...

    Regards,
    Elias



    elathan@phys.uoa.gr Guest

  6. #6

    Default Re: convert a C/C++ array to a Ruby array

    uoa.gr wrote in message news:<uoa.gr>... 
    > >
    > > If you can get the length from Foo, you can use it.[/ref]
    >
    > Yes I know. The problem is that I am creating Ruby bindings for a large
    > framework (more than 50 C++ classes) and the bindings code is automagically
    > generated by reading the C++ prototypes, using a handmade Ruby
    > pr/compiler.
    >
    > The generated code is quite readable and can be altered by a human, but
    > I want to eliminate as much as I can the human intervation, so as the majority of the
    > C++ prototypes to be compiled in the Ruby API without human editing.
    >[/ref]
    <snip>
     


    I'm just curious, why didn't you use SWIG for this? Sounds sort of
    like you're recreating SWIG.

    Phil
    Phil Guest

  7. #7

    Default Re: convert a C/C++ array to a Ruby array

    On Fri, Jan 09, 2004 at 06:46:40AM +0900, Phil Tomson wrote: 
    > I'm just curious, why didn't you use SWIG for this? Sounds sort of
    > like you're recreating SWIG.[/ref]

    Because SWIG doesn't produce readable by human C++ code. Also, there
    are things in the extension that I prefer to port to Ruby in a different
    way from the one SWIG uses.

    I know that using SWIG's internal scripting language can do almost
    everything I do, but I already know C++ and it is easiest to do it
    in my way. I don't recreate SWIG. My auto-generated code is specific
    for my extension and the compiler I create is less than 500 lines of
    Ruby code (I don't p the prototypes, because the original C++ code
    has RTTI related functions i.e. 'bring me the arguments foo() takes
    as input').

    Regards,
    --
    University of Athens I bet the human brain
    Physics Department is a kludge --Marvin Minsky



    Elias Guest

Similar Threads

  1. Convert image into byte array
    By harshalsuri in forum Coldfusion - Advanced Techniques
    Replies: 1
    Last Post: July 12th, 12:34 PM
  2. Convert CF list of dates to javascript array
    By Conti in forum Coldfusion - Getting Started
    Replies: 2
    Last Post: June 4th, 10:47 PM
  3. how to convert xml response into array
    By BalajiKumar in forum Macromedia Flex General Discussion
    Replies: 3
    Last Post: March 29th, 03:52 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139