From: Emil Ong Date: 2001-07-04T06:18:38+09:00 Subject: [ruby-talk:17197] Re: Iterators in C This worked great! Thanks again, Emil On Wed, 4 Jul 2001, ts wrote: > >>>>> "E" == Emil Ong writes: > > E> an array. I looked at rb_iterate() and its description in the pickaxe > E> book, but I don't understand how to use it except with rb_each(). > > A stupid example : > > pigeon% cat tt.c > #include > > static VALUE > tt_each(tmp) > VALUE tmp; > { > rb_funcall(tmp, rb_intern("map!"), 0, 0); > } > > static VALUE > tt_yield(obj) > VALUE obj; > { > int i = NUM2INT(obj); > rb_warn("received %d", i); > return (i <= 2)?Qtrue:Qfalse; > } > > static VALUE > tt_map_bang(obj) > VALUE obj; > { > return rb_iterate(tt_each, obj, tt_yield, 0); > } > > void Init_tt() > { > rb_define_method(rb_cArray, "tt!", tt_map_bang, 0); > } > pigeon% > > pigeon% ruby -rtt -e 'a = [1, 3, 4, 2]; a.tt!; p a' > -e:1: warning: received 1 > -e:1: warning: received 3 > -e:1: warning: received 4 > -e:1: warning: received 2 > [true, false, false, true] > pigeon% > > > > Guy Decoux >