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