From: Ken Bloom Date: 2007-12-31T12:39:58+09:00 Subject: Re: Using "sort!" in a C extension (1.9 problem) On Sat, 29 Dec 2007 19:30:31 -0500, Andre Nathan wrote: > On Sun, 2007-12-30 at 08:06 +0900, Andre Nathan wrote: >> On Sun, 2007-12-30 at 07:34 +0900, Siep Korteling wrote: >> > Like this? >> > a=[[2],[1,2,3],[1],[1,2]] >> > p a.sort_by{|ar|ar.length} >> >> Yes but in a C extension. The code I have is not working with 1.9. > > Well not exactly. sort_by works but I'd prefer to use sort! with a > block, as it does the sorting in-place. > > In 1.9, if I inspect "ary" below, I only see the current array element. > In 1.8, it is an array of the two elements which are being compared. > > static VALUE > sort_i(VALUE ary) > { > long v1 = FIX2LONG(RARRAY_PTR(ary)[0]); long v2 = > FIX2LONG(RARRAY_PTR(ary)[1]); return LONG2FIX(v1 - v2); > } > > Andre This appears to be the result of a ruby 1.9 semantic change for block arguments. (from eigenclass.org's summary) > News semantics for block arguments > > |v| now works like the former |v,|: > > [RUBY_VERSION, RUBY_RELEASE_DATE] # => ["1.8.5", "2006-08-25"] > def m; yield 1, 2; end > m{|v| v} # => [1, 2] # !> > multiple values for a block parameter (2 for 1) > > vs. > > [RUBY_VERSION, RUBY_RELEASE_DATE] # => ["1.9.0", "2007-08-03"] > def m; yield 1, 2; end > m{|v| v} # => 1 So sort_i needs to be revised to take two parameters, as follows: static VALUE sort_i(VALUE first, VALUE second) { long v1 = FIX2LONG(first); long v2 = FIX2LONG(second); return LONG2FIX(v1 - v2); } (Note that I have not tested this, and I have not programmed a C extension ever without the help of SWIG.) --Ken -- Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/