Created
October 12, 2010 08:58
-
-
Save bakkdoor/621876 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "mkmf" | |
create_makefile("printer") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bakkdoor:~/projekte/ruby/c-ext> ruby extconf.rb | |
creating Makefile | |
bakkdoor:~/projekte/ruby/c-ext> make | |
gcc -shared -o printer.so printer.o -L. -L/usr/lib -L. -Wl,-Bsymbolic-functions -rdynamic -Wl,-export-dynamic -lruby1.8 -lpthread -lrt -ldl -lcrypt -lm -lc | |
bakkdoor:~/projekte/ruby/c-ext> irb | |
irb(main):001:0> require "printer" | |
=> true | |
irb(main):002:0> p = Printer.new | |
=> #<Printer:0x7fd6227f4c70> | |
irb(main):003:0> p.println "fooo bar baz!" | |
fooo bar baz! | |
=> nil | |
irb(main):004:0> p.println [1,2,3] | |
1 | |
2 | |
3 | |
=> nil | |
irb(main):005:0> p.println [1,2,3].inspect | |
[1, 2, 3] | |
=> nil |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ruby.h> | |
static VALUE printer_init(VALUE self) | |
{ | |
return self; | |
} | |
static VALUE printer_println(VALUE self, VALUE obj) | |
{ | |
rb_funcall(self, rb_intern("puts"), 1, obj); | |
} | |
VALUE printer_class; | |
void Init_printer() | |
{ | |
printer_class = rb_define_class("Printer", rb_cObject); | |
rb_define_method(printer_class, "initialize", printer_init, 0); | |
rb_define_method(printer_class, "println", printer_println, 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment