From: btricha@... Date: 2008-05-07T11:01:26+09:00 Subject: Re: Yet another question about extending ruby with C Hi Albert, Thanks for responding. Sorry for not making things clear and easy to follow... I was in a hurry when I wrote it. :/ I'll try and clear things up when I have a chance. Thanks again! -- Bryan On 5/6/08, Albert Schlef wrote: > Bryan Richardson wrote: > > Say I have the following: > > > > typedef struct { > > char* name; > > } Foo; > > > > [...] > > > > static VALUE foo_init(VALUE self) { > > VALUE str; > > char* name = "Bryan"; > > str = rb_str_new2(name); > > rb_iv_set(self, "@name", str); > > return self; > > } > > > > static VALUE test(VALUE self, VALUE arg) { > > Foo* f; > > Data_Get_Struct(arg, Foo, f); > > printf("Name: %s\n", f->name); > > return Qnil; > > } > > > > [...] > > > > If I do the following, everything works perfectly and I see my name > > printed > > on the screen: > > > > [...] > > b.test(f) // prints "Bryan" > > "works perfectly"? I don't see how it can print "Bryan", for two > reasons: > > 1. It's supposed to print "Name: Bryan", not "Bryan". > > 2. But it can't print "Name: Bryan" either. That's because in foo_init() > you aren't settings f->name to "Bryan". Instead, you're settings an > instance variable, "@name", to "Bryan". It happens that "@name" has a > similar name to char *name, but these two variables aren't related at > all. > > Last but not least: > > If you want ppl to help you, you must make it easier for them to help > you. Do that by choosing _sane_ names for your > methods/structures/variables. I had a hard time following your code (and > that in your previous question). 'test', 'Test' and 'Foo' aren't > meaningful names. Change them to make_person, Person, and person_rec. > -- > Posted via http://www.ruby-forum.com/. > >