From: nobu.nokada@... Date: 2002-11-30T12:25:00+09:00 Subject: Re: rb_ary_each() Howto? Hi, At Sat, 30 Nov 2002 11:38:17 +0900, Squidster wrote: > Rubyists, > I tried to find some sample code using rb_ary_each() but failed... > Anyway, I wanted to write the 'C' equivalent of the following simple Ruby code: > > k = 0 > foo = ["foo","bar","baz"] > foo.each { |c| > c.push(k) > print c, "\n" > k += 1 > } #include "ruby.h" static VALUE print_c(VALUE c, int *kp) { /* c.push(k) # String has no push method */ rb_str_concat(c, rb_obj_as_string(INT2FIX(*kp))); /* c << k.to_s */ rb_io_puts(1, &c, rb_defout); /* puts c */ *kp += 1; /* k += 1 */ return Qnil; } int main(void) { int k = 0; static const char *const foo_init[] = {"foo","bar","baz",0}; const char *const *s = foo_init; VALUE foo; ruby_init(); for (foo = rb_ary_new2(3); *s; ++s) { rb_ary_push(foo, rb_str_new2(*s)); } rb_iterate(rb_each, foo, print_c, (VALUE)&k); rb_p(foo); printf("k=%d\n", k); return 0; } -- Nobu Nakada