On Sun, 25 Jul 2004 13:36:54 +0900, daz <dooby / d10.karoo.co.uk> wrote:
> 
> You don't show how you defined 'add_values' or how you're calling it
> from Ruby, but I think your initial array is in 'self' ...

Here is a more complete example of what I'm trying to do.

> Here's an example from enum.c (Enumerable#reject)
> with some printf() statements added.

I'm afraid I didn't understand how to translate your example into mine.

This is basically what I'm trying to do:

-------8<------ Bitmask.c
#include "ruby.h"

static VALUE
bm_new(VALUE class)
{
  long* bitmask;
  bitmask = ALLOC(long);
  *bitmask = 0;
  VALUE data = Data_Wrap_Struct(class, 0, free, bitmask);
  return data; 
}

static VALUE
bm_add_bit(VALUE self, VALUE bit)
{
  long* bitmask;
  int int_bit = NUM2INT(bit);
  printf("in to add_bit: %s %s\n", 
      RSTRING(rb_inspect(self))->ptr,
      RSTRING(rb_inspect(bit))->ptr);
  Data_Get_Struct(self, long, bitmask);
  *bitmask |= 1 << int_bit;
  return self; 
}

static VALUE
bm_add_bits(VALUE self, VALUE bits)
{
  printf("in to add_bits: %s\n", RSTRING(rb_inspect(bits))->ptr);
  rb_iterate(rb_each, bits, bm_add_bit, 1);
  return self; 
}

static VALUE
bm_value(VALUE self)
{
  long* bitmask;
  Data_Get_Struct(self, long, bitmask);
  return INT2NUM(*bitmask);
}

VALUE cBitmask;

void Init_Bitmask() {
  cBitmask = rb_define_class("Bitmask", rb_cObject);
  rb_define_singleton_method(cBitmask, "new", bm_new, 0);
  rb_define_method(cBitmask, "add_bit", bm_add_bit, 1);
  rb_define_method(cBitmask, "add_bits", bm_add_bits, 1);
  rb_define_method(cBitmask, "value", bm_value, 0);
}


-------8<------ test_Bitmask.rb

require 'Bitmask'

bm = Bitmask.new
bm.add_bit(2)
bm.add_bit(4)
puts bm.value

bm = Bitmask.new
bm.add_bits([2, 4])
puts bm.value