From: daz Date: 2004-06-05T17:48:38+09:00 Subject: Re: Extending in C: need to use a non-basic class defined in Ruby in my C code Austin McDonald wrote: > Hi, I'm attempting to transfer some intensive functions from Ruby to C, > but I'd like to put as little of my code in C as possible. So, I'm > trying to access a class defined entirely in Ruby from my C code. > Specifically, I need to create an instance of this class (called > Population) and access some of its methods. A few questions about this: > > 1) Do the functions I need to use have to be implemented in C as well, > or can I access their "ruby version" using some C functions? What C > functions do I need to use to do this? > Seemless beauty is what you'll find :-) Define an empty class (if its name is known) in the Init_foo part of your extension - look at any other extension to get clues (rb_define_class). Reopen your class (looks the same as a class definition) and define your methods inside it (all in Ruby): class Foo def a # whatever end def b end end Call them from C using the rb_funcall family. > 2) Is there a decent C API document anywhere? I've read through > "Programming Ruby," and its chapter on extending in C seems adequate for > getting started, but leaves out a lot and simply refers one to the > horribly unreadable header files, which give little to no indication as > to what each function does. > Co-author of "Programming Ruby," (and so much more relating to Ruby) Dave Thomas has documented the API in the .c files if you grab a reasonably up-to-date copy of the source distribution. > And, on a related note: > > 3) What kind of IDE have you found to be good for co-development of > Ruby/C code? I'm currently working with Dev-C++ (www.bloodshed.net), > and with some tweaking it seems usable. Is there anything better? Ooooh, that was sneaky; you just want to ensure that this thread grows to 35+ replies, but the downside is that no-one gets round to answering your main questions. My answer is: You /know/ there are better IDE's that Dev-C++, even free ones, but there isn't just one for Ruby and I use a low-priced shareware one on Windows which is an anagram of UtralEdti and has ctag support, macros, blah ... Everyone else uses their favourite and they're all better than mine, OK. > > Thanks very much, > Thank you for choosing Ruby ;-) daz