From: Mikkel Bruun Date: 2001-10-19T04:14:40+09:00 Subject: [ruby-talk:22720] Re: SV: Re: blocks in C??? (and hi guys) thanks alot... it looks like just what i needed... hopefully i will have the time to look more into it this weekend... regards Mikkel -----Original Message----- From: ts To: ruby-talk@ruby-lang.org Cc: ruby-talk@ruby-lang.org Sent: 17-10-2001 17:41 Subject: [ruby-talk:22676] Re: SV: Re: blocks in C??? (and hi guys) >>>>> "M" == Mikkel Bruun writes: M> obj.method(parm1){ M> |retval1| M> dostuff(retval1) M> } Well, here an example, the method Tt#foo receive an object and call the method #each of this object. The block given to the method #each is tt_yield (N.B. : there is a faster way to retrieve rb_defout :-))) pigeon% cat tt.c #include static VALUE tt_defout; static VALUE tt_iter(tmp) VALUE tmp; { return rb_funcall2(tmp, rb_intern("each"), 0, 0); } static VALUE tt_yield(ary) VALUE ary; { int i; rb_io_write(tt_defout, rb_str_new2("Received : ")); if (TYPE(ary) == T_ARRAY) { for (i = 0; i < RARRAY(ary)->len; i++) { rb_io_write(tt_defout, rb_inspect(RARRAY(ary)->ptr[i])); rb_io_write(tt_defout, rb_str_new2(" ")); } } else { rb_io_write(tt_defout, rb_inspect(ary)); } rb_io_write(tt_defout, rb_str_new2("\n")); return Qnil; } static VALUE tt_foo(obj, ary) VALUE obj, ary; { rb_iterate(tt_iter, ary, tt_yield, 0); return obj; } void Init_tt() { VALUE tt_cTt = rb_define_class("Tt", rb_cObject); tt_defout = rb_gvar_get(rb_global_entry(rb_intern("$defout"))); rb_define_method(tt_cTt, "foo", tt_foo, 1); } pigeon% pigeon% cat b.rb #!/usr/bin/ruby require 'tt' a = Tt.new a.foo [12, 24, 36] a.foo({"a" => 12, "b" => "c", "d" => [1, 2]}) pigeon% pigeon% b.rb Received : 12 Received : 24 Received : 36 Received : "a" 12 Received : "b" "c" Received : "d" [1, 2] pigeon% Guy Decoux