Skip to content

Instantly share code, notes, and snippets.

@mattwildig
Created June 22, 2011 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattwildig/1041242 to your computer and use it in GitHub Desktop.
Save mattwildig/1041242 to your computer and use it in GitHub Desktop.
*.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