From: matz@... (Yukihiro Matsumoto) Date: 2001-11-07T15:15:37+09:00 Subject: [ruby-talk:24486] Re: C Extension Help Hi, In message "[ruby-talk:24465] C Extension Help" on 01/11/07, Jason Voegele writes: |I'm trying to write some additional collection classes for Ruby, such |as Sets, Trees, Bags, Linked Lists, etc. I've got a working Ruby |version for some of them, but I'm currently attempting to implement a |Set (backed by a Ruby Hash) as a C extension, but I'm not much of a C |programmer and I got a run-time error I can't figure out. rb_iterate calls block_proc with arguments (element, arg), so that set_add is called as set_add(element, 0). So I modified your source to: #include "ruby.h" static VALUE init_i(VALUE item, VALUE self) { VALUE hash = rb_iv_get(self, "@store"); rb_funcall(hash, rb_intern("store"), 2, item, item); return item; } static VALUE set_initialize(int argc, VALUE *argv, VALUE self) { VALUE elements; VALUE store = rb_hash_new(); rb_iv_set(self, "@store", store); rb_scan_args(argc, argv, "01", &elements); if (RTEST(elements)) { rb_iterate(rb_each, elements, init_i, self); } return self; } Init_set() { VALUE set = rb_define_class( "Set", rb_cObject); rb_define_method(set, "initialize", set_initialize, -1); } And it seems to work. matz.