Good way to write a C extension to Ruby?

Hi,

Whats a good way to write a C extension to Ruby?

I’m aware that there’s a chapter about in the Pickaxe book (I think),
and will be checking that out, and also SWIG, if it supports Ruby. But
looking for replies from anyone who has already tried any of these
ways, and their feedback.

Thanks

On Wed, 2006-07-12 at 01:45 +0900, vasudevram wrote:

Hi,

Whats a good way to write a C extension to Ruby?

I’m aware that there’s a chapter about in the Pickaxe book (I think),
and will be checking that out, and also SWIG, if it supports Ruby. But
looking for replies from anyone who has already tried any of these
ways, and their feedback.

The Pickaxe chapter is pretty good. Best yet, go to the RAA, grab a
simple extension, and dissect.

Finding living examples of extconf.rb is really good.

Writing C extensions for ruby isn’t that hard unless you get hit by GC
problems, where your memory gets claimed, but it’s not so hard.

My ruby-epeg module took about 4 hours to write, and is pretty
understandable.

Aria

vasudevram wrote:

Hi,

Whats a good way to write a C extension to Ruby?

I’m aware that there’s a chapter about in the Pickaxe book (I think),
and will be checking that out, and also SWIG, if it supports Ruby. But
looking for replies from anyone who has already tried any of these
ways, and their feedback.

Thanks

A terse but invaluable reference: README.EXT in the Ruby source distro.
Also, there are many great examples under ext/ in the distro.

On 7/11/06, vasudevram [email protected] wrote:

Whats a good way to write a C extension to Ruby?

I would just use the Ruby API. Unless you have a specific need to
provide bindings for multiple languages, SWIG is really adding, not
removing complexity. Write the smallest extension you can and build it
incrementally. The previously mentioned readme.ext and pick axe
chapters are excellent, you may also find some value in the Ruby
Hacking Guide (partially translated at: http://rhg.rubyforge.org/).

Good luck and if you get stuck, ask again
pth

Marcin MielżyÅ?ski wrote:

Swig is brilliant and has very good support for Ruby, it will help you
automate many tasks - it has it’s own DSL (argh…). Actually I used it
to wrap my C++ code. It is also worth mentioning that Ruby has the most
consistent and easy to understand/use c api I’ve seen (Lua’s api is also
very clear) - just look into Ruby sources: array.c and hash.c - you’ll
realize what I’m talking about ;D

I’ll second all of that. Understanding README.EXT and some small
examples is the best first step to using SWIG or writing an extension
from scratch.

Maybe the OP could give us some more details about the nature of the
extension… wrapping a library? defining methods that work with ruby
classes in a more efficient way that calling ruby methods? a new data
structure implemented from scratch?

Thanks! I’ll check what you suggest.

it has it’s own DSL (argh…).
Not clear - why the “argh” for the DSL? Meaning Domain Specific
Language?

Vasudev

On Thu, 2006-07-13 at 01:50 +0900, vasudevram wrote:

Thanks! I’ll check what you suggest.

it has it’s own DSL (argh…).
Not clear - why the “argh” for the DSL? Meaning Domain Specific
Language?

I, too, find the DSL frustrating. It’s trying to write a C-Ruby
extension in a language that is neither C nor Ruby.

Aria

vasudevram wrote:

Thanks! I’ll check what you suggest.

it has it’s own DSL (argh…).
Not clear - why the “argh” for the DSL? Meaning Domain Specific
Language?

Right.

It is called an interface file in which you can #include c/c++
type/function definitions and do some further refinements (like exposing
only some functions, change visibility of c++ class members, specialize
templates, etc).

lopex

vasudevram wrote:

Hi,

Whats a good way to write a C extension to Ruby?

I’m aware that there’s a chapter about in the Pickaxe book (I think),
and will be checking that out, and also SWIG, if it supports Ruby. But
looking for replies from anyone who has already tried any of these
ways, and their feedback.

Swig is brilliant and has very good support for Ruby, it will help you
automate many tasks - it has it’s own DSL (argh…). Actually I used it
to wrap my C++ code. It is also worth mentioning that Ruby has the most
consistent and easy to understand/use c api I’ve seen (Lua’s api is also
very clear) - just look into Ruby sources: array.c and hash.c - you’ll
realize what I’m talking about ;D

lopex

Thanks for the answers, all - very helpful!

Maybe the OP could give us some more details about the nature of the
extension… wrapping a library? defining methods that work with ruby

Wrapping some simple C functions … as a way to start learning how to
write Ruby C extensions. Was thinking to do this with my selpg.c
utility (
http://www-128.ibm.com/developerworks/opensource/library/l-clutil/ -
see the Resources section at end of this URL) which is currently a
standalone C program; I plan to cut out the main function, just wrap
the core page-selecting logic in the process_input() function as a
Ruby/C extension. The process_args() function could be rewritten in
Ruby since all it does is argument checking.

Thanks
Vasudev Ram
Business web site: http://www.dancingbison.com
PDF conversion toolkit: http://sourceforge.net/projects/xtopdf
---------------------------------------------------------------------------­---------------

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs