[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Good way to write a C extension to Ruby?

vasudevram

7/11/2006 4:42:00 PM


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
------------------------------------------------------------------------------------------
Vasudev Ram
Business web site: http://www.dancin...
PDF conversion toolkit: http://sourceforge.net/proje...
------------------------------------------------------------------------------------------

9 Answers

Aria Stewart

7/11/2006 4:50:00 PM

0

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


Francis Cianfrocca

7/11/2006 5:26:00 PM

0

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.

--
Posted via http://www.ruby-....

Patrick Hurley

7/11/2006 6:58:00 PM

0

On 7/11/06, vasudevram <vasudevram@gmail.com> 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.ruby...).

Good luck and if you get stuck, ask again
pth

Marcin Mielzynski

7/11/2006 10:17:00 PM

0

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

Joel VanderWerf

7/12/2006 1:31:00 AM

0

Marcin MielżyÅ?ski wrote:
> 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

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?

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

vasudevram

7/12/2006 4:45:00 PM

0


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

Marcin Mielzynski 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
>
> lopex

Aria Stewart

7/12/2006 4:56:00 PM

0

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



Marcin Mielzynski

7/12/2006 9:34:00 PM

0

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

7/14/2006 6:51:00 AM

0


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... -
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.dancin...
PDF conversion toolkit: http://sourceforge.net/proje...
---------------------------------------------------------------------------­---------------