public
Created

  • Download Gist
.gitignore
1 2 3 4
*.o
*.so
*.bundle
Makefile
ext/bar.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#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);
}
ext/extconf.rb
Ruby
1 2 3 4 5
require 'mkmf'
 
$CFLAGS << " -Wall"
 
create_makefile('foo')
lib/foo.rb
Ruby
1 2 3 4 5 6 7 8 9
module Foo
class Bar
def original_ruby_method
"original"
end
end
end
 
require 'foo.so'
test.rb
Ruby
1 2 3 4 5 6 7 8 9 10
$:.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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.