using Comparable in C

Discussion in 'Ruby' started by teeler@gmail.com, Aug 23, 2006.

  1. Guest

    Is there a way i can use the functions defined in compar.c (der, like
    without copying them)?

    I'd like to be able to arbitrarily compare VALUE's - do i just have to
    imitate that code? Assuming that the underlying type has Comparable
    mixed in, would I just have to do what compar.c does, something like:

    cmp_ge(x, y)
    VALUE x, y;
    {
    VALUE c = rb_funcall(x, cmp, 1, y);

    if (NIL_P(c)) return cmperr();
    if (rb_cmpint(c, x, y) >= 0) return Qtrue;
    return Qfalse;
    }

    and the same for the rest (le, eq, etc.) ?
    It'd be nice if i could just call cmp_ge(VALUE, VALUE) but I realize
    it's not in the C api...
     
    , Aug 23, 2006
    #1
    1. Advertising

  2. On 8/23/06, <> wrote:
    > Is there a way i can use the functions defined in compar.c (der, like
    > without copying them)?
    >
    > I'd like to be able to arbitrarily compare VALUE's - do i just have to
    > imitate that code? Assuming that the underlying type has Comparable
    > mixed in, would I just have to do what compar.c does, something like:
    >
    > cmp_ge(x, y)
    > VALUE x, y;
    > {
    > VALUE c = rb_funcall(x, cmp, 1, y);
    >
    > if (NIL_P(c)) return cmperr();
    > if (rb_cmpint(c, x, y) >= 0) return Qtrue;
    > return Qfalse;
    > }
    >
    > and the same for the rest (le, eq, etc.) ?
    > It'd be nice if i could just call cmp_ge(VALUE, VALUE) but I realize
    > it's not in the C api...


    You can just use rb_funcall;

    if (RTEST(rb_funcall(object, rb_intern("=="), 1, other_object))) {
    //...
    }

    A new thing I just learned, for single character operators like '<'
    and '>' you can just do this;

    if (RTEST(rb_funcall(object, '<', 1, other_object))) {
    //...
    }

    If I was going to be doing this type of thing a lot I'd rewrite the
    compar.c functions, returning int instead of VALUE. For example;

    #define my_cmperr() (rb_cmperr(x, y), 0)

    int
    my_cmp_ge(VALUE x, VALUE y)
    {
    VALUE c = rb_funcall(x, cmp, 1, y);

    if (NIL_P(c)) return my_cmperr();
    if (rb_cmpint(c, x, y) >= 0) return 1;
    return 0;
    }

    Just my personal opinion.

    Cheers,
    Dave
     
    David Balmain, Aug 23, 2006
    #2
    1. Advertising

  3. Guest

    I figured thats what I'd have to do - i was trying to save myself the
    work.

    Thanks!
    Tyler
     
    , Aug 23, 2006
    #3
    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. Replies:
    5
    Views:
    6,890
  2. steve

    implementing comparable

    steve, Nov 16, 2004, in forum: Java
    Replies:
    2
    Views:
    465
    steve
    Nov 18, 2004
  3. Bergholt

    Generics and Comparable

    Bergholt, Nov 27, 2004, in forum: Java
    Replies:
    3
    Views:
    6,061
    Bergholt
    Nov 30, 2004
  4. Eddy C
    Replies:
    1
    Views:
    2,337
    Roedy Green
    Sep 2, 2005
  5. Pedro Côrte-Real

    Weird elusive bug using Comparable

    Pedro Côrte-Real, Jul 31, 2006, in forum: Ruby
    Replies:
    10
    Views:
    199
    Robert Klemme
    Aug 1, 2006
Loading...

Share This Page