Ruby Programming/C Extensions

      C Extensions

      Extending ruby with C extensions is relatively easy. Here is a tutorial in basic extension creation. Here is the README about converting ruby to c types and vice-versa.

      Typically a C extension looks like

      file go.c

      #include "ruby.h"
      
      void Init_go() {
      
      }
      

      Then you create a makefile for it by using the mkmf library.


      Differences between 1.9 and 1.8

      1.9 has at least the difference of having more macros defined. Here's how to get them in 1.8 (some of this from Phusion passenger's code).

      
      #ifndef RARRAY_LEN
              #define RARRAY_LEN(ary) RARRAY(ary)->len
      #endif
      #ifndef RSTRING_PTR
              #define RSTRING_PTR(str) RSTRING(str)->ptr
      #endif
      #ifndef RSTRING_LEN
              #define RSTRING_LEN(str) RSTRING(str)->len
      #endif
      
      #ifndef RBIGNUM_DIGITS
          #define RBIGNUM_DIGITS(obj) RBIGNUM(obj)->digits
      #endif
      
      

      C extensions in Jruby

      You can use java extensions in jruby (obviously). You can also use ffi and/or ffi-inliner gem to use native C extensions.

      External Links

      A tutorial. A rundown.

      Last modified on 13 October 2010, at 16:00