Ruby Programming/C Extensions

From Wikibooks, open books for an open world
< Ruby Programming
Jump to: navigation, search

Contents

[edit] 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.


[edit] 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

[edit] 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.

[edit] External Links

A tutorial. A rundown.