passing a func to a ruby func

Discussion in 'Ruby' started by elathan@phys.uoa.gr, Dec 9, 2003.

  1. Guest

    Hello!

    Can I export a function such as qsort() of the Linux stdlib to a Ruby method. qsort()
    takes a C function pointer as a parameter.

    I guess that a similiar behaviour can be achieved using blocks. Is it the right way to
    procceed?

    Any hints will be appreciated

    Regards,
    Elias

    PS. qsort() is an example, I am not trying to export qsort to Ruby. :)
    , Dec 9, 2003
    #1
    1. Advertising

  2. ts Guest

    >>>>> "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

    svg% cat aa.c
    #include <ruby.h>

    static int
    aa_sort(const void *va, const void *vb)
    {
    if (rb_block_given_p()) {
    return NUM2INT(rb_yield(rb_assoc_new(*(VALUE *)va, *(VALUE *)vb)));
    }
    return NUM2INT(rb_funcall(*(VALUE *)va, rb_intern("<=>"), 1,
    *(VALUE *)vb));
    }

    static VALUE
    aa_qsort(VALUE obj, VALUE a)
    {
    a = rb_Array(a);
    qsort(RARRAY(a)->ptr, RARRAY(a)->len, sizeof(VALUE), aa_sort);
    return a;
    }



    void Init_aa()
    {
    rb_define_global_function("qsort!", aa_qsort, 1);
    }
    svg%

    svg% ruby -raa -e 'p qsort!([3,6,8,1,0])'
    [0, 1, 3, 6, 8]
    svg%

    svg% ruby -raa -e 'p qsort!([3,6,8,1,0]) {|a, b| b <=> a}'
    [8, 6, 3, 1, 0]
    svg%


    Guy Decoux
    ts, Dec 9, 2003
    #2
    1. Advertising

  3. 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,
    --
    University of Athens I bet the human brain
    Physics Department is a kludge --Marvin Minsky
    Elias Athanasopoulos, Dec 9, 2003
    #3
  4. Tim Hunter Guest

    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'.
    Tim Hunter, Dec 9, 2003
    #4
  5. On Wed, Dec 10, 2003 at 08:12:00AM +0900, Tim Hunter wrote:
    > 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'.


    Thanks. I can now get the whole picture.

    Regards,
    --
    University of Athens I bet the human brain
    Physics Department is a kludge --Marvin Minsky
    Elias Athanasopoulos, Dec 13, 2003
    #5
  6. On Wed 10 Dec 2003 at 00:29:36 +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


    [snip]

    Guy, I think it's about time you wrote a book about Ruby for the rest of
    us :)

    Ian
    --
    Ian Macdonald | You will be awarded a medal for
    System Administrator | disregarding safety in saving someone.
    |
    http://www.caliban.org |
    |
    Ian Macdonald, Dec 15, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CRON
    Replies:
    24
    Views:
    200,644
    Adrienne Boswell
    Jun 20, 2006
  2. Johnny
    Replies:
    3
    Views:
    453
    Robert Kern
    Aug 23, 2005
  3. Hari Sekhon
    Replies:
    0
    Views:
    501
    Hari Sekhon
    Jun 20, 2006
  4. Vinko Vrsalovic

    int func() v/s int func(void)

    Vinko Vrsalovic, Jan 21, 2005, in forum: C Programming
    Replies:
    14
    Views:
    1,303
    Villy Kruse
    Jan 24, 2005
  5. Alex Vinokur
    Replies:
    6
    Views:
    352
    Tor Rustad
    Nov 18, 2006
Loading...

Share This Page