# Using Comparable in C

**URL:** <https://www.ruby-forum.com/t/using-comparable-in-c/66062>\
**Category:** Ruby\
**Created:** [August 23, 2006, 3:46am UTC](https://www.ruby-forum.com/t/using-comparable-in-c/66062 "2006-08-23T03:46:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2006, 3:46am UTC](https://www.ruby-forum.com/t/using-comparable-in-c/66062/1 "2006-08-23T03:46:21Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2006, 8:04am UTC](https://www.ruby-forum.com/t/using-comparable-in-c/66062/2 "2006-08-23T08:04:45Z")

</div>

On 8/23/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2006, 5:55pm UTC](https://www.ruby-forum.com/t/using-comparable-in-c/66062/3 "2006-08-23T17:55:50Z")

</div>

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

Thanks!  
Tyler
