Skip to content

Instantly share code, notes, and snippets.

@radarek
Created June 15, 2009 11:17
Show Gist options
  • Save radarek/130051 to your computer and use it in GitHub Desktop.
Save radarek/130051 to your computer and use it in GitHub Desktop.
require "mkmf"
create_makefile("null")
#include <stdlib.h>
#include <ruby.h>
VALUE string_with_null(VALUE self) {
char str[] = { 102, 111, 111, 0, 98, 97, 114 };
return rb_str_new(str, 7);
}
void Init_null() {
rb_define_global_function("string_with_null", string_with_null, 0);
}
$ ruby extconf.rb
creating Makefile
$ make
gcc -I. -I/opt/ruby-enterprise-1.8.6-20090520/lib/ruby/1.8/i686-linux -I/opt/ruby-enterprise-1.8.6-20090520/lib/ruby/1.8/i686-linux -I. -D_FILE_OFFSET_BITS=64 -fPIC -g -O2 -c null.c
gcc -shared -o null.so null.o -L. -L/opt/ruby-enterprise-1.8.6-20090520/lib -Wl,-R/opt/ruby-enterprise-1.8.6-20090520/lib -L. -rdynamic -Wl,-export-dynamic -ldl -lcrypt -lm -lc
$ ruby test.rb
foobar
7
require "null"
puts string_with_null
puts string_with_null.size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment