Writing Your Own Ruby Extension With C
In case the title of this post is giving you deja-vu, we've looked at "How To Create A Ruby Extension In C" before at Ruby Inside. We've also looked at using RubyInline to make the process even faster by not having to write a specialized C extension.
Despite this, Wim Vander Schelden has written such a detailed and interesting guide called "Writing Your Own Ruby Extension With C" that it's a must-read if this area interests you, especially since it goes far beyond what was covered in our previous articles.
January 25, 2007 at 2:52 pm
Also check the excellent Ruby Extension utility classes on Gaffer on Games (http://www.gaffer.org/archives/category/ruby/). It is a really nice toolkit to create your C++ ruby objects easily.
January 25, 2007 at 5:59 pm
His comments on arrays are completely wrong. You do not need to declare an extra RARRAY struct and then assign to it, as he does in the greet function. If the user passed an array, then that's what you get. Or, you can force the issue by setting the number of arguments in the function declaration to -2 to get splat-like behavior.
If an array is passed as the argument, just use RARRAY(arg)->ptr. To get at a specific index, just use RARRAY(arg)->ptr[i].
Dan