From: Craig Files Date: 2001-07-26T07:19:14+09:00 Subject: [ruby-talk:18552] yield in SWIG Hi, After getting such great responses after my last posting, I am hoping that I can get an opinion about the following code segment using yield in SWIG to overload the each() method. SWIG.i file : %module Foo %{ #include "Foo.h" %} class Bar { public: int data1, data2; Bar() : data1(0), data2(0) {} }; class Foo { public: Bar *BarPtr; // Foo(); }; %addmethods Foo { void each () { rb_yield(Wrap_Bar(cBar, self->BarPtr)); // return a valid Ruby object } } The opinion that I am looking for is the use of the Wrap_Bar , which is defined in SWIG. The Wrap_Bar segment can be rewritten as the following (extracting what Wrap_Bar defines): if (self->BarPtr) { rb_yield(Data_Wrap_Struct(cBar, 0, 0, self->BarPtr)); } So, what I want to know is if this is the best way to get rb_yield to return a valid ruby object? Does this do garbage collection correct, etc? Is there an easier way? ... I must admit that I like this solution, but I know that it is going to be used a lot, so I want to make sure that it is the best solution. Thanks, Craig -- ___________________________________________________________________________ Craig Files craig_files@agilent.com