passing a func to a ruby func

E

elathan

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. :)
 
T

ts

e> 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

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
 
E

Elias Athanasopoulos

e> 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,
 
T

Tim Hunter

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,

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'.
 
E

Elias Athanasopoulos

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'.

Thanks. I can now get the whole picture.

Regards,
 
I

Ian Macdonald

e> 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

[snip]

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

Ian
--
Ian Macdonald | You will be awarded a medal for
System Administrator | disregarding safety in saving someone.
(e-mail address removed) |
http://www.caliban.org |
|
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top