Ruby extension tutorial

Wim Vander Schelden schreef:

Hi,

I just finished a tutorial about writing simple ruby extensions.
I was hoping you guys could give me some feedback on what you think about it.

If you find any mistakes (code, spelling, grammar and explanation), I would also appreciate it if you told me about them?

I'm looking for critisism - I hope to improve the article.

kind regards,

Wim

Silly me, I forgot to paste the link, here you go:

http://nanoblog.ath.cx/index.rb?module=readmore&id=8

- Wim

Hey Wim,

Change "fase" to "phase".

Also, I think there is a typo in the opening. Where you say
"Ruby is slow", I think what you mean is "Ruby is an extremely
powerful and flexible language". It's an understandable mistake,
all of the keys are so close to each other. :wink:

Thanks for the section and examples on Ruby mark-and-sweep, this
is something I've been having some trouble with.

Garth

Wim Vander Schelden wrote:

···

Wim Vander Schelden schreef:

>
>> Hi,
>>
>> I just finished a tutorial about writing simple ruby extensions.
>> I was hoping you guys could give me some feedback on what you think
>> about it.
>>
>> If you find any mistakes (code, spelling, grammar and explanation), I
>> would also appreciate it if you told me about them?
>>
>> I'm looking for critisism - I hope to improve the article.
>>
>> kind regards,
>>
>> Wim
>>
> Silly me, I forgot to paste the link, here you go:
>
> http://nanoblog.ath.cx/index.rb?module=readmore&id=8

I like the content of the tutorial, and I've been curious about writing C extensions for a while.

One thing, though, is that I believe you have misspelled phase and fase.

Dan

Wim Vander Schelden wrote:

···

Wim Vander Schelden schreef:

Hi,

I just finished a tutorial about writing simple ruby extensions.
I was hoping you guys could give me some feedback on what you think about it.

If you find any mistakes (code, spelling, grammar and explanation), I would also appreciate it if you told me about them?

I'm looking for critisism - I hope to improve the article.

kind regards,

Wim

Silly me, I forgot to paste the link, here you go:

http://nanoblog.ath.cx/index.rb?module=readmore&id=8

- Wim

Wim Vander Schelden wrote:

Wim Vander Schelden schreef:

Hi,

I just finished a tutorial about writing simple ruby extensions.
I was hoping you guys could give me some feedback on what you think about it.

If you find any mistakes (code, spelling, grammar and explanation), I would also appreciate it if you told me about them?

I'm looking for critisism - I hope to improve the article.

Nice work! Here's a couple of nitpicks...

You recommend using the "rb_" prefix on classes, modules, etc., but I'd argue that rb_-prefixed names should be reserved for things defined by Ruby itself, and your extension should use some other naming convention.

You use names_array->ptr[i] to access elements in an array. My choice would be to use rb_ary_index instead, to put a little logical distance between my code and the RArray structure definition. You could use rb_ary_length to get the array size as well.

Regarding "Where to go from here," another good learning resource is the C sources for the builtin classes. I frequently refer to string.c, array.c, io.c, etc. to see how things should be done.

I'd change this to:

abort 'need stdio.h' unless have_header("stdio.h")

but other than that, thank you, thank you, thank you for using an example that doesn't proceed to write a Makefile when the right thingies aren't found. I think about half the C extensions I've installed puke somewhere after attempting to build rather than puking cleanly in extconf.rb.

···

On Jan 21, 2007, at 16:10, Wim Vander Schelden wrote:

Wim Vander Schelden schreef:

I just finished a tutorial about writing simple ruby extensions.
I was hoping you guys could give me some feedback on what you think about it.

If you find any mistakes (code, spelling, grammar and explanation), I would also appreciate it if you told me about them?

http://nanoblog.ath.cx/index.rb?module=readmore&id=8

exit unless have_header("stdio.h")

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!

Thank you all for your comments!

Change "fase" to "phase".

Doh. Changed.

Also, I think there is a typo in the opening. Where you say
"Ruby is slow", I think what you mean is "Ruby is an extremely
powerful and flexible language". It's an understandable mistake,
all of the keys are so close to each other. :wink:

I fixed that, that always happens to me, my keyboard must have been low on batteries

Thanks for the section and examples on Ruby mark-and-sweep, this
is something I've been having some trouble with.

Your welcome :slight_smile:

You recommend using the "rb_" prefix on classes, modules, etc., but I'd argue that rb_-prefixed names should be reserved for things defined by Ruby itself, and your extension should use some other naming convention.

You're right, I've changed that now.

You use names_array->ptr[i] to access elements in an array. My choice would be to use rb_ary_index instead, to put a little logical distance between my code and the RArray structure definition. You could use rb_ary_length to get the array size as well.

Hmmm I never even heard of that, would probably be better. I'll look into it after my exams, and if I like it, I'll change it.

Regarding "Where to go from here," another good learning resource is the C sources for the builtin classes. I frequently refer to string.c, array.c, io.c, etc. to see how things should be done.

You're right, I added that.

One item on my wishlist would be to show (in perhaps a sidebar) how to
use mkrf instead of mkmf.

I haven't heard of mkrf yet, but I'll also check that out after my exams. Thanks for the suggestion.

Wim

Eric Hodel schreef:

···

On Jan 21, 2007, at 16:10, Wim Vander Schelden wrote:

Wim Vander Schelden schreef:

I just finished a tutorial about writing simple ruby extensions.
I was hoping you guys could give me some feedback on what you think about it.

If you find any mistakes (code, spelling, grammar and explanation), I would also appreciate it if you told me about them?

http://nanoblog.ath.cx/index.rb?module=readmore&id=8

exit unless have_header("stdio.h")

I'd change this to:

abort 'need stdio.h' unless have_header("stdio.h")

but other than that, thank you, thank you, thank you for using an example that doesn't proceed to write a Makefile when the right thingies aren't found. I think about half the C extensions I've installed puke somewhere after attempting to build rather than puking cleanly in extconf.rb.

--Eric Hodel - drbrain@segment7.net - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!

Yes, I first did that as well, I thought have_header and the like aborted all by themselves. I changed it to abort 'need stdio.h' now, looks cleaner when it fails.

Kind regards,

Wim

It allows you to look for headers that may have different names for different versions like:

have_header "foo1.h" or
   have_header "foo2.h" or
   abort "can't find headers for foo"

···

On Jan 22, 2007, at 01:46, Wim Vander Schelden wrote:

Eric Hodel schreef:

I'd change this to:

abort 'need stdio.h' unless have_header("stdio.h")

Yes, I first did that as well, I thought have_header and the like aborted all by themselves. I changed it to abort 'need stdio.h' now, looks cleaner when it fails.

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!

Eric Hodel schreef:

···

On Jan 22, 2007, at 01:46, Wim Vander Schelden wrote:

Eric Hodel schreef:

I'd change this to:

abort 'need stdio.h' unless have_header("stdio.h")

Yes, I first did that as well, I thought have_header and the like aborted all by themselves. I changed it to abort 'need stdio.h' now, looks cleaner when it fails.

It allows you to look for headers that may have different names for different versions like:

have_header "foo1.h" or
  have_header "foo2.h" or
  abort "can't find headers for foo"

--Eric Hodel - drbrain@segment7.net - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!

I might add this to the discussion of the extconf.rb file later, I think it deserves a little extra attention.

- Wim