A
Andre Nathan
Hello
In ruby 1.8, I could use Array#sort! in a C extension like this:
static VALUE
call_sort_bang(VALUE ary)
{
return rb_funcall(ary, rb_intern("sort!"), 0);
}
static VALUE
sort_i(VALUE ary)
{
long v1 = FIX2LONG(RARRAY_PTR(ary)[0]);
long v2 = FIX2LONG(RARRAY_PTR(ary)[1]);
return LONG2FIX(v1 - v2);
}
static VALUE
foo(void)
{
VALUE ary = rb_ary_new();
rb_ary_push(ary, INT2FIX(1));
rb_ary_push(ary, INT2FIX(0));
rb_ary_push(ary, INT2FIX(2));
rb_iterate(call_sort_bang, ary, sort_i, 0);
rb_funcall(rb_mKernel, rb_intern("p"), 1, ary);
return a;
}
This would print "[0, 1, 2]" as expected. However, it doesn't work in
ruby1.9 compiled from svn (checked out today). The problem seems to be
that only one of the array elements is being passed to sort_i(), instead
of a pair of elements as it's done in 1.8.
What is the correct way to do this in 1.9?
Thanks in advance,
Andre
In ruby 1.8, I could use Array#sort! in a C extension like this:
static VALUE
call_sort_bang(VALUE ary)
{
return rb_funcall(ary, rb_intern("sort!"), 0);
}
static VALUE
sort_i(VALUE ary)
{
long v1 = FIX2LONG(RARRAY_PTR(ary)[0]);
long v2 = FIX2LONG(RARRAY_PTR(ary)[1]);
return LONG2FIX(v1 - v2);
}
static VALUE
foo(void)
{
VALUE ary = rb_ary_new();
rb_ary_push(ary, INT2FIX(1));
rb_ary_push(ary, INT2FIX(0));
rb_ary_push(ary, INT2FIX(2));
rb_iterate(call_sort_bang, ary, sort_i, 0);
rb_funcall(rb_mKernel, rb_intern("p"), 1, ary);
return a;
}
This would print "[0, 1, 2]" as expected. However, it doesn't work in
ruby1.9 compiled from svn (checked out today). The problem seems to be
that only one of the array elements is being passed to sort_i(), instead
of a pair of elements as it's done in 1.8.
What is the correct way to do this in 1.9?
Thanks in advance,
Andre