Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
how easy it is to create and load a C extension in ruby

create a new dir

$ mkdir /tmp/ruby
$ cd /tmp/ruby

create extconf.rb

require 'mkmf'
create_makefile 'wat'

create c source

#include <ruby.h>
#include <stdio.h>

  puts("hello, world!");

create makefile

$ ruby extconf.rb 
creating Makefile
$ ls
Makefile   extconf.rb wat.c

run make

$ make
compiling wat.c
linking shared-object wat.bundle
$ ls
Makefile   extconf.rb wat.bundle wat.c      wat.o

load extension

$ irb
>> require './wat'
hello, world!
=> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment