Ryan Davis
3/25/2007 6:31:00 PM
Your question has already been answered. Alternatively, use inline to
learn without all the hassle (no makefile, no builds, no type
conversions, no time wasted). Just try out what you want to do
straight up in ruby with inlined C, and then go look at what the C
code is producing:
#!/usr/local/bin/ruby
require 'rubygems'
require 'inline'
class PKey
inline do |builder|
builder.c <<-EOM
static void bit(int n) {
printf("Fun %d\\n", n);
}
EOM
end
end
k = PKey.new
k.bit(5)
############################################################
# Produces:
#
# #include "ruby.h"
#
# # line 9 "./blah.rb"
# static VALUE bit(VALUE self, VALUE _n) {
# int n = FIX2INT(_n);
#
# printf("Fun %d\n", n);
# return Qnil;
# }
#
# #ifdef __cplusplus
# extern "C" {
# #endif
# void Init_Inline_PKey_f671() {
# VALUE c = rb_cObject;
# c = rb_const_get_at(c,rb_intern("PKey"));
# rb_define_method(c, "bit", (VALUE(*)(ANYARGS))bit, 1);
#
# }
# #ifdef __cplusplus
# }
# #endif