On Apr 4, 2005, at 8:01 AM, Tom Copeland wrote: > On Mon, 2005-04-04 at 23:19 +0900, Joe Van Dyk wrote: > >> Also, I couldn't really figure out how to only have one function >> in C, >> the documentation seemed to say that I needed to have a class? >> > > You can attach the function to Object: Please note that :test is a kernel method. :) I'd like to illustrate an alternative to Tom's suggestion: > =============================== > $ cat example.c > #include <stdio.h> > #include "ruby.h" > static VALUE test(VALUE self) { > printf("HI!\n"); > return Qnil; > } > void Init_example() { > rb_define_method(rb_cObject, "test", test, 0); > } $ cat example.rb require 'inline' class Example inline do |builder| builder.include "<stdio.h>" builder.c <<-"END" void do_it() { puts("HI!"); } END end end > $ cat extconf.rb > require 'mkmf' > create_makefile("example") skip this file entirely > $ ruby extconf.rb && make > [... some gcc output ...] skip this step entirely - and I should add, by just running/requiring the code everything is silently done for you, as necessary, very very quickly. > $ irb irb > irb(main):001:0> require 'example' > => true > irb(main):002:0> test Example.new.do_it # because test is rude. :) > HI! > => nil > irb(main):003:0> There is no middle step. For developer time and convenience, ruby inline kicks ass!