From: Tim Hunter Date: 2003-12-10T08:12:00+09:00 Subject: Re: passing a func to a ruby func On Wed, 10 Dec 2003 03:29:56 +0900, Elias Athanasopoulos wrote: > On Wed, Dec 10, 2003 at 12:29:36AM +0900, ts wrote: >> >>>>> "e" == elathan writes: >> >> e> Can I export a function such as qsort() of the Linux stdlib to a >> Ruby e> method. qsort() takes a C function pointer as a parameter. >> >> Not really understood what you want to do, but for qsort a stupid >> example > > Well, I am trying to create a ruby method call for a C++ method which > takes as an argument a pointer to a user-defined function. I.e. the C++ > method is like: > > foo (int a, int b, double (*f) (double *, double *), int c); > > Thanks for you qsort() example. I'll try to study it and see what I can > get from. My main worry, is that the 'f' function which is passed in > foo() above might be more complicated than a common Ruby block. > > Regards, Presumably `foo' calls the function pointed to by `f' and you want `f' to point to a Ruby method. The way to do this is to write an intermediate C function `func'. Pass `func's address to `foo'. When `foo' calls `func', `func' translates the C arguments to Ruby objects and calls the appropriate Ruby method via rb_funcall or something. Or, `func' could call a user-supplied Proc object. `func' can in turn convert the value returned from whatever it calls to a C type and then return that value to `foo'.