Good way to write a C extension to Ruby?

V

vasudevram

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

Aredridel

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
 
F

Francis Cianfrocca

vasudevram said:
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.
 
P

Patrick Hurley

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
 
M

Marcin Mielżyński

vasudevram said:
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
 
J

Joel VanderWerf

Marcin said:
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?
 
A

Aredridel

Thanks! I'll check what you suggest.

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
 
M

Marcin Mielżyński

vasudevram said:
Thanks! I'll check what you suggest.

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
 
V

vasudevram

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top