Phil Tomson wrote:
> This generating C on the fly concept sounds interesting, can you go into 
> more detail?  Any code you can post?

Phil,

It's in the cgen library on RAA:
   http://www.ruby-lang.org/en/raa-list.rhtml?name=CGenerator

The name is a bit unfortunate. Cgen is not a code generator in the sense 
of a compiler back-end. What it does is manage the mundane aspects of 
defining C functions and using the Ruby C interface. In this respect, 
it's like an inline. But, unlike inline, it also has a templating aspect 
that allows you to incrementally add code to methods and members to 
structs. This feature is used in the CShadow module to simplify defining 
classes wrapped around C structs that have inheritance of the struct 
members. Struct members can be basic C types or references to Ruby objects.

A too-simple example:

require 'cgen/cshadow'

class MyComplex < Numeric
   include CShadow

   shadow_attr_accessor :re => "double re", :im => "double im"

   def initialize re, im
     self.re = re
     self.im = im
   end

   m = define_method(:mul!) {
     arguments :other
     declare :other_shadow => "MyComplex_Shadow *other_shadow"
     declare :new_re => "double new_re", :new_im => "double new_im"
     body %{
       // Your C code here. Full example is included in tarball.
       // you can refer to shadow attrs like so: shadow->re
     }
     returns "self"
   }

   # It's not useful in this example, but you can add code
   # to the "body" of #mul! as follows:
   m.body %{
     // more C code
   }
end

MyComplex.commit # generate the lib and load it

z = MyComplex.new 5, 1.3
z.mul! 10
p z.re
p z.im