From: Austin McDonald Date: 2004-06-05T18:01:38+09:00 Subject: Re: Extending in C: need to use a non-basic class defined in Ruby in my C code Thanks for the excellent reply - I was thinking I'd have to do something along those lines. I'm pleased to know that it works, though, and that it's fairly intuitive. And thanks for the tip on the source code; I'm sure it'll be useful in the future. (I'm sure these jokes have all been made by this point, but Dave Thomas? Isn't he the founder of Wendy's?) And it wasn't my intention to start a lengthy thread about IDEs; I was just hoping for something that could do both Ruby and C with equal skill, or even in the same IDE, (or really, anything decent for Ruby at all :) ) Oh well, it'll give the night-owls like myself something to post about when they can't sleep. As I guess is apparent from my questions, I'm pretty new to Ruby, and I've been very impressed with how easy it is to do things in this language. My only complaint (thus far) is the insane slowness; hence, my desire to write C extensions. Hopefully that route will pan out for me. Thanks again, Austin McDonald daz wrote: >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 > > > > > > >