From: Tim Hunter Date: 2002-10-24T07:41:46+09:00 Subject: Re: On Using Data_Wrap_Struct If Bar is just some data structure, you'll need to convert both n and the string pointed to by msg to Ruby objects and then store them in a Struct object that corresponds to your Bar structure. Then push the Struct object onto the array and store the VALUE that represents the array in the Foo structure. Don't forget to mark the array VALUE within Foo's mark function. You can create a Struct class that corresponds to your Bar structure using rb_struct_define, like this: VALUE Class_Bar; Class_Bar = rb_struct_define(NULL, "n", "msg", 0); This is the C equivalent of the Ruby statement Bar = Struct.new(nil, "n", "msg"); To make a Ruby Bar object equivalent to an instance of the C Bar structure (assuming msg is a null-delimited string): Bar c_bar = {123, "a msg"}; VALUE ruby_bar; ruby_bar = rb_funcall(Class_Bar, rb_intern("new"), 2, INT2FIX(c_bar.n), rb_str_new2(c_bar.msg)); Use rb_ary_push to push ruby_bar onto the end of your array. Check out Chap 17 in the Pickaxe. On Thu, 24 Oct 2002 05:23:27 +0900, Mark Probert wrote: > > Rubyists, > > In my extension, I have a C struct like > > struct _bar { > int n; > char *msg; > } Bar; > > I have a defined class Foo in the extension. > Foo has an array member called @arr. > > I want to be able to push values of Bar onto > @arr and to get at them from my ruby code, > something like: > > f = Foo.new > f.run # --> runs and adds Bars to @arr > f.arr.each { |bar| puts "#{bar.n} --> #{bar.msg}" } > > I think that I need to use Data_Wrap_Struct, however > I am not sure. > > Guidance, anyone? > > Thanks, once again. > > > -mark.