Create a gist now

Instantly share code, notes, and snippets.

*.o
*.so
*.bundle
Makefile
#include "ruby.h"
static VALUE Foo;
static VALUE Bar;
static VALUE
print_string(VALUE self, VALUE string) {
printf("%s\n", StringValuePtr(string));
return Qnil;
}
void Init_foo() {
Foo = rb_define_module("Foo");
Bar = rb_define_class_under(Foo, "Bar", rb_cObject);
rb_define_method(Bar, "print_string", print_string, 1);
}
require 'mkmf'
$CFLAGS << " -Wall"
create_makefile('foo')
module Foo
class Bar
def original_ruby_method
"original"
end
end
end
require 'foo.so'
$:.unshift 'lib'
$:.unshift 'ext'
require 'foo'
bar = Foo::Bar.new
bar.print_string "hello" #defined in C (bar.c)
puts bar.original_ruby_method #defined in Ruby (foo.rb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment