From: Eero Saynatkari Date: 2006-01-21T12:44:36+09:00 Subject: Re: Extending ruby with c ruby talk wrote: > I am using the pickaxe example to help learn how it is writen. > Thank you once again. > Becker > > my run program > [code] > require "C:\\ruby\\Test" > t = Test.new() > t.add("Bill Chase") > [code] > > my code example from pickaxe saved as a .rb This here is the problem; the below code must be saved as a .c file and then compiled to a library using extconf.rb (the Pickaxe should have further instructions for this). Once done, you will have a Test.so file that you can #require for example from irb. > [code] > #include "ruby.h" > static VALUE t_init(VALUE self) > { > VALUE arr; > arr = rb_ary_new(); > rb_iv_set(self, "@arr", arr); > return self; > } > > static VALUE t_add(VALUE self, VALUE anObject) > { > VALUE arr; > arr = rb_iv_get(self, "@arr"); > rb_ary_push(arr, anObject); > return arr; > } > > VALUE cTest; > > void Init_Test() { > cTest = rb_define_class("Test", rb_cObject); > rb_define_method(cTest, "initialize", t_init, 0); > rb_define_method(cTest, "add", t_add, 1); > } > > [code] > > > < Error messages elided /> The code itself is fine, you just need to compile it. E -- Posted via http://www.ruby-forum.com/.