From: Andre Nathan Date: 2007-12-30T07:12:03+09:00 Subject: Re: Using "sort!" in a C extension (1.9 problem) My example was a bit contrieved because I could just have used #sort without a block. What I'm actually doing is sorting an array by the length of its elements, which are also arrays. That's why I need the block version of #sort. On Sun, 2007-12-30 at 02:49 +0900, Andre Nathan wrote: > 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 > >