Passing a func to a ruby func

Hello!

Can I export a function such as qsort() of the Linux stdlib to a Ruby method. qsort()
takes a C function pointer as a parameter.

I guess that a similiar behaviour can be achieved using blocks. Is it the right way to
procceed?

Any hints will be appreciated

Regards,
Elias

PS. qsort() is an example, I am not trying to export qsort to Ruby. :slight_smile:

Can I export a function such as qsort() of the Linux stdlib to a Ruby
method. qsort() takes a C function pointer as a parameter.

Not really understood what you want to do, but for qsort a stupid example

svg% cat aa.c
#include <ruby.h>

static int
aa_sort(const void *va, const void *vb)
{
    if (rb_block_given_p()) {
        return NUM2INT(rb_yield(rb_assoc_new(*(VALUE *)va, *(VALUE *)vb)));
    }
    return NUM2INT(rb_funcall(*(VALUE *)va, rb_intern("<=>"), 1,
                              *(VALUE *)vb));
}

static VALUE
aa_qsort(VALUE obj, VALUE a)
{
    a = rb_Array(a);
    qsort(RARRAY(a)->ptr, RARRAY(a)->len, sizeof(VALUE), aa_sort);
    return a;
}

void Init_aa()
{
    rb_define_global_function("qsort!", aa_qsort, 1);
}
svg%

svg% ruby -raa -e 'p qsort!([3,6,8,1,0])'
[0, 1, 3, 6, 8]
svg%

svg% ruby -raa -e 'p qsort!([3,6,8,1,0]) {|a, b| b <=> a}'
[8, 6, 3, 1, 0]
svg%

Guy Decoux

Well, I am trying to create a ruby method call for a C++ method which
takes as an argument a pointer to a user-defined function. I.e. the C++ method
is like:

foo (int a, int b, double (*f) (double *, double *), int c);

Thanks for you qsort() example. I’ll try to study it and see what I can
get from. My main worry, is that the ‘f’ function which is passed in foo()
above might be more complicated than a common Ruby block.

Regards,

···

On Wed, Dec 10, 2003 at 12:29:36AM +0900, ts wrote:

Can I export a function such as qsort() of the Linux stdlib to a Ruby
method. qsort() takes a C function pointer as a parameter.

Not really understood what you want to do, but for qsort a stupid example

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

[snip]

Guy, I think it’s about time you wrote a book about Ruby for the rest of
us :slight_smile:

Ian

···

On Wed 10 Dec 2003 at 00:29:36 +0900, ts wrote:

Can I export a function such as qsort() of the Linux stdlib to a Ruby
method. qsort() takes a C function pointer as a parameter.

Not really understood what you want to do, but for qsort a stupid example

–
Ian Macdonald | You will be awarded a medal for
System Administrator | disregarding safety in saving someone.
ian@caliban.org |
http://www.caliban.org |
>

Presumably foo' calls the function pointed to by f’ and you want f' to point to a Ruby method. The way to do this is to write an intermediate C function func’. Pass func's address to foo’. When foo' calls func’,
func' translates the C arguments to Ruby objects and calls the appropriate Ruby method via rb_funcall or something. Or, func’ could call
a user-supplied Proc object. func' can in turn convert the value returned from whatever it calls to a C type and then return that value to foo’.

···

On Wed, 10 Dec 2003 03:29:56 +0900, Elias Athanasopoulos wrote:

On Wed, Dec 10, 2003 at 12:29:36AM +0900, ts wrote:

Can I export a function such as qsort() of the Linux stdlib to a
Ruby e> method. qsort() takes a C function pointer as a parameter.

Not really understood what you want to do, but for qsort a stupid
example

Well, I am trying to create a ruby method call for a C++ method which
takes as an argument a pointer to a user-defined function. I.e. the C++
method is like:

foo (int a, int b, double (*f) (double *, double *), int c);

Thanks for you qsort() example. I’ll try to study it and see what I can
get from. My main worry, is that the ‘f’ function which is passed in
foo() above might be more complicated than a common Ruby block.

Regards,

Thanks. I can now get the whole picture.

Regards,

···

On Wed, Dec 10, 2003 at 08:12:00AM +0900, Tim Hunter wrote:

Presumably foo' calls the function pointed to by f’ and you want f' to point to a Ruby method. The way to do this is to write an intermediate C function func’. Pass func's address to foo’. When foo' calls func’,
func' translates the C arguments to Ruby objects and calls the appropriate Ruby method via rb_funcall or something. Or, func’ could call
a user-supplied Proc object. func' can in turn convert the value returned from whatever it calls to a C type and then return that value to foo’.

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