ruby extension best practices

L

leon breedt

hi,

i am about to create an extension for a third party API (API is pretty
consistent), was wondering what the best practices are.

are there tools for generating the glue for methods on classes i
create to wrap the various abstractions?

is there something like Pyrex for Ruby? (google didn't really come up
with anything conclusive).

what is the most "modern" way of creating extensions without making
too much work for myself such that it is difficult to maintain?

when is it a good idea to use Ruby/DL and when not?

thanks
leon
 
J

Joel VanderWerf

leon said:
hi,

i am about to create an extension for a third party API (API is pretty
consistent), was wondering what the best practices are.

are there tools for generating the glue for methods on classes i
create to wrap the various abstractions?

is there something like Pyrex for Ruby? (google didn't really come up
with anything conclusive).

what is the most "modern" way of creating extensions without making
too much work for myself such that it is difficult to maintain?

when is it a good idea to use Ruby/DL and when not?

Check out SWIG. It can do most of the work for you, generating wrappers
for the 3rd party API.

Another possibility, is cgen (on RAA) which I wrote to provide a ruby
interface to the ruby C API. It was intended for code generation rather
than wrapping, so it might be a better choice if you are generating an
extension from scratch. I have used it with an existing library, though.
 
P

Phil Tomson

hi,

i am about to create an extension for a third party API (API is pretty
consistent), was wondering what the best practices are.

are there tools for generating the glue for methods on classes i
create to wrap the various abstractions?

is there something like Pyrex for Ruby? (google didn't really come up
with anything conclusive).

How about RubyInline?
http://rubyforge.org/projects/rubyinline/
what is the most "modern" way of creating extensions without making
too much work for myself such that it is difficult to maintain?

when is it a good idea to use Ruby/DL and when not?

If your 3rd party shared library is written in C and not C++, Ruby/DL might be
the way to go since, as you say the library is well documented.

Phil
 
L

leon breedt

thanks for all the suggestions, SWIG seems to be closest to what i
want (the others seem to be fine for small amounts of interfacing, but
i want a fair amount of API coverage and with library data types
mapped to Ruby classes).

in short, i just want something that does the
Data_Wrap_Struct/Data_Get_Struct magic for me, as well as the C<->Ruby
type marshaling. SWIG would appear to have this as a goal, buti'm
having some problems with it.

the library uses opaque structs for the most part.

i can't seem to indicate that i want an opaque struct of a particular
type to be mapped to a Ruby class, and methods taking the opaque
struct pointer as an argument to become instance methods of that
class.

i tried this using %extend, but that errors out when given an opaque
struct, it wants a struct body, defeating the whole point of using
SWIG, and breaking the signature of library methods.

anyone have any experience with SWIG used like this?

leon
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top