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 ™ Ruby
extension treat such a prototype?

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

Regards,
Elias

Hi,

···

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

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.

How do you use it in C at all?


Nobu Nakada

Quoting nobu.nokada@softhome.net:

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.

How do you use it in C at all?

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

Hi,

···

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

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.

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

What do you really intend, pseudo array?


Nobu Nakada

Quoting nobu.nokada@softhome.net:

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.

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

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
parser/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.

What do you really intend, pseudo array?

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 wrote in message news:1073567518.3ffd571e18e36@webmail.uoa.gr

Quoting nobu.nokada@softhome.net:

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.

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

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
parser/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.

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

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

Phil

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 parse the prototypes, because the original C++ code
has RTTI related functions i.e. ‘bring me the arguments foo() takes
as input’).

Regards,

···

On Fri, Jan 09, 2004 at 06:46:40AM +0900, Phil Tomson wrote:

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.

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

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


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