[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Yet another question about extending ruby with C

Bryan Richardson

5/6/2008 12:50:00 PM

Hello all,

I'm still working on figuring how how to extend ruby with C libraries.
Hope someone can help me with this one!

Say I have the following:

typedef struct {
char* name;
} Foo;

static VALUE rb_cBar;
static VALUE rb_cTest;

static VALUE foo_new(VALUE self) {
Foo* f;
VALUE info;
info = Data_Make_Struct(rb_cTest, Foo, 0, free, f);
rb_obj_call_init(info, 0, 0);
return info;
}

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;
}

void Init_power_flow() {
rb_cBar = rb_define_class("Bar", rb_cObject);
rb_cTest = rb_define_class("Test", rb_cObject);
rb_define_method(rb_cBar, "test", test, 1);
rb_define_method(rb_cBar, "new_foo", foo_new, 0);
rb_defien_method(rb_cTest, "initialize", foo_init, 0);
}

If I do the following, everything works perfectly and I see my name
printed
on the screen:

b = Bar.new
f = b.new_foo
b.test(f) // prints "Bryan"

However, say I change the foo_new method to be the following:

static VALUE foo_new(VALUE self) {
Foo* f;
VALUE info;
f = ALLOC(Foo);
f->name = "Bryan";
info = Data_Wrap_Struct(self, 0, free, f);
return info;
}

and I change the method declaration in Init to be the following:

rb_define_method(rb_cTest, "initialize", foo_new, 0);

Now, when I do what I did before, I get an error:

b = Bar.new
f = Test.new
b.test(f) // TypeError: wrong argument type Test (expected Data)

Any idea why trying to define a constructor rather than a 'factory'
method is causing me issues?

--
Thanks in advance!
Bryan
--
Posted via http://www.ruby-....

3 Answers

Albert Schlef

5/7/2008 1:52:00 AM

0

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-....

Bryan Richardson

5/7/2008 2:01:00 AM

0

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 <albertschlef@gmail.com> 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-....
>
>

David Fritzinger

12/13/2011 8:26:00 PM

0

In article
<Jason-1312111227130001@67-150-174-202.lsan.mdsg-pacwest.com>,
Jason@nospam.com (Jason) wrote:

> In article <dfritzin-DEB9A8.06501113122011@news.eternal-september.org>,
> David Fritzinger <dfritzin@nospamtome.hotmail.com> wrote:
>
> > In article
> > <Jason-1212112119030001@67-150-173-198.lsan.mdsg-pacwest.com>,
> > Jason@nospam.com (Jason) wrote:
> >
> > > In article <dfritzin-BAE102.18533412122011@news.eternal-september.org>,
> > > David Fritzinger <dfritzin@nospamtome.hotmail.com> wrote:
[snip]
> > > > Sigh... You have been told this before. It wasn't scientists who
> > > > misidentified that (or, I suppose those) fossils. It was a poor forgery
> > > > put together by a Chinese farmer who was trying to get more money
> > > > selling the fossils. It fooled National Geographic, but it didn't fool
> > > > real scientists when they looked at it. It was clear to them that it
> > > > was
> > > > two separate fossils put together, and it turns out that both are
> > > > evolutionarily interesting wrt the transition from dinosaurs to birds.
> > > > Note: No creationists had anything to do with the discovery of that
> > > > farmer's fraud.
> > > >
> > > > For more information, see: http://en.wikipedia.org/wiki/Arc...
> > >
> > > Are you saying the info. in National Geographic magazine can't be trusted
> > > to be correct?
> >
> > Certainly in this case, they made a mistake. After all, NG isn't a
> > science journal, but is a combination of a popular science magazine
> > along with geography. It is clear they rushed to print their article,
> > and didn't let the actual scientists make up their minds on what the
> > fossils were.
> >
> > Can they be trusted? Certainly far more so than the serial liars at the
> > ICR and AiG, but they aren't real scientists, so they can make mistakes
> > if they rush to print something before the real scientists have reached
> > a consensus.
>
> The magazine does have some excellent pictures which is one of the main
> reason I subscribed to it for about 5 years. The articles were also
> interesting.

Your inability to respond to the actual point I made is noted.