From: daz Date: 2004-06-06T05:28:37+09:00 Subject: Re: Extending in C: need to use a non-basic class defined in Ruby in my C code Joel VanderWerf wrote: > daz wrote: > > Austin McDonald wrote: > > > >>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? > >> > > > > 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. > > This isn't going to help much, if at all, with the speed of these calls, > since it still goes thru the usual method lookup. Maybe that's ok, if > the bottleneck is not these calls, but the code around them. > I should have said that. To gain any speed benefit from the extension, you'd have to cut the intensive bits out of Ruby code and write them in C. You might use rb_define_method() / rb_define_singleton_method() when building your class instead of creating an empty class. More help on the Ruby API is in README.EXT in the source distro. ----- The profile library (described in Programming Ruby under "Standard Libraries") can help you to track down where the bottlenecks are in your Ruby script and you should be able to find ways of reducing the need for C. require 'profile' # at the top of your script With experience, you'll find ways to avoid making Ruby do unnecessary, time-consuming work like object duplication. daz