# Re: Ruby extension tutorial

**URL:** <https://www.ruby-forum.com/t/re-ruby-extension-tutorial/82866>\
**Category:** Ruby\
**Created:** [January 22, 2007, 1:15am UTC](https://www.ruby-forum.com/t/re-ruby-extension-tutorial/82866 "2007-01-22T01:15:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wim\_Vander\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/13edae/32.png) [@Wim\_Vander\_S](https://www.ruby-forum.com/u/Wim_Vander_S)\
**Post date:** [January 22, 2007, 1:15am UTC](https://www.ruby-forum.com/t/re-ruby-extension-tutorial/82866/1 "2007-01-22T01:15:31Z")

</div>

Wim Vander S. schreef:

> kind regards,
> 
> Wim

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

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

- Wim

---

<div class="post-metadata">

**Author:** ![Wim\_Vander\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/13edae/32.png) [@Wim\_Vander\_S](https://www.ruby-forum.com/u/Wim_Vander_S)\
**Post date:** [January 22, 2007, 1:24am UTC](https://www.ruby-forum.com/t/re-ruby-extension-tutorial/82866/2 "2007-01-22T01:24:32Z")

</div>

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

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

Garth

Wim Vander S. wrote:

> Wim Vander S. 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](http://nanoblog.ath.cx/index.rb?module=readmore&id=8)

---

<div class="post-metadata">

**Author:** ![Wim\_Vander\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/13edae/32.png) [@Wim\_Vander\_S](https://www.ruby-forum.com/u/Wim_Vander_S)\
**Post date:** [January 22, 2007, 2:10am UTC](https://www.ruby-forum.com/t/re-ruby-extension-tutorial/82866/3 "2007-01-22T02:10:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wim\_Vander\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/13edae/32.png) [@Wim\_Vander\_S](https://www.ruby-forum.com/u/Wim_Vander_S)\
**Post date:** [January 22, 2007, 2:21am UTC](https://www.ruby-forum.com/t/re-ruby-extension-tutorial/82866/4 "2007-01-22T02:21:37Z")

</div>

Wim Vander S. wrote:

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

---

<div class="post-metadata">

**Author:** ![Wim\_Vander\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/13edae/32.png) [@Wim\_Vander\_S](https://www.ruby-forum.com/u/Wim_Vander_S)\
**Post date:** [January 22, 2007, 9:40am UTC](https://www.ruby-forum.com/t/re-ruby-extension-tutorial/82866/5 "2007-01-22T09:40:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wim\_Vander\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/13edae/32.png) [@Wim\_Vander\_S](https://www.ruby-forum.com/u/Wim_Vander_S)\
**Post date:** [January 22, 2007, 10:46am UTC](https://www.ruby-forum.com/t/re-ruby-extension-tutorial/82866/6 "2007-01-22T10:46:32Z")

</div>

Eric H. schreef:

> cleanly in extconf.rb.
> 
> –Eric H. - removed\_email\_address@domain.invalid - [http://blog.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

---

<div class="post-metadata">

**Author:** ![Wim\_Vander\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/13edae/32.png) [@Wim\_Vander\_S](https://www.ruby-forum.com/u/Wim_Vander_S)\
**Post date:** [January 22, 2007, 11:05am UTC](https://www.ruby-forum.com/t/re-ruby-extension-tutorial/82866/7 "2007-01-22T11:05:35Z")

</div>

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

> Eric H. 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 H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)

I LIT YOUR GEM ON FIRE!

---

<div class="post-metadata">

**Author:** ![Wim\_Vander\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/13edae/32.png) [@Wim\_Vander\_S](https://www.ruby-forum.com/u/Wim_Vander_S)\
**Post date:** [January 22, 2007, 10:20am UTC](https://www.ruby-forum.com/t/re-ruby-extension-tutorial/82866/8 "2007-01-22T10:20:30Z")

</div>

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

> Wim Vander S. 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](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 H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)

I LIT YOUR GEM ON FIRE!

---

<div class="post-metadata">

**Author:** ![Wim\_Vander\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/13edae/32.png) [@Wim\_Vander\_S](https://www.ruby-forum.com/u/Wim_Vander_S)\
**Post date:** [January 22, 2007, 11:13am UTC](https://www.ruby-forum.com/t/re-ruby-extension-tutorial/82866/9 "2007-01-22T11:13:56Z")

</div>

Eric H. schreef:

> It allows you to look for headers that may have different names for

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

- Wim
