[Ruby] splatting arguments in a C extension

Ryan Davis ryand-ruby at zenspider.com
Sat Feb 2 01:09:21 PST 2008


On Feb 2, 2008, at 00:46 , Ryan Davis wrote:

> I'm not sure you can.

I am teh awesome:

#!/usr/bin/ruby -w

require 'rubygems'
require 'inline'

class X
   def m a, b, c
     p [a, b, c]
   end

   inline do |builder|
     builder.add_type_converter("ID", '', '')

     builder.c_raw '
       VALUE splat_funcall(VALUE recv, ID mid, int n, VALUE args) {
         VALUE *argv = ALLOC_N(VALUE, n); // pretty sure this is GC  
friendly
         int i;

         for (i = 0; i < n; i++) {
           argv[i] = rb_ary_entry(args, i);
         }

         return rb_funcall2(recv, mid, n, argv);
       }
     '

     builder.c '
       void x(VALUE ary) {
         splat_funcall(self, rb_intern("m"), 3, ary);
       }
     '
   end
end

X.new.x([1, 2, 3])



More information about the Ruby mailing list