Module: Comparable

Included in:
File::Stat, Numeric, String, Time
Defined in:
compar.c

Instance Method Summary collapse

Instance Method Details

#<(other) ⇒ Boolean

Compares two objects based on the receiver's <=> method, returning true if it returns -1.

Returns:

  • (Boolean)


# File 'compar.c'

static VALUE
cmp_lt(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;
}

#<=(other) ⇒ Boolean

Compares two objects based on the receiver's <=> method, returning true if it returns -1 or 0.

Returns:

  • (Boolean)


# File 'compar.c'

static VALUE
cmp_le(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;
}

#==(other) ⇒ Boolean

Compares two objects based on the receiver's <=> method, returning true if it returns 0. Also returns true if obj and other are the same object.

Returns:

  • (Boolean)


# File 'compar.c'

static VALUE
cmp_equal(x, y)
    VALUE x, y;
{
    VALUE a[2];

    if (x == y) return Qtrue;

    a[0] = x; a[1] = y;
    return rb_rescue(cmp_eq, (VALUE)a, cmp_failed, 0);
}

#>(other) ⇒ Boolean

Compares two objects based on the receiver's <=> method, returning true if it returns 1.

Returns:

  • (Boolean)


# File 'compar.c'

static VALUE
cmp_gt(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;
}

#>=(other) ⇒ Boolean

Compares two objects based on the receiver's <=> method, returning true if it returns 0 or 1.

Returns:

  • (Boolean)


# File 'compar.c'

static VALUE
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;
}

#between?(min, max) ⇒ Boolean

Returns false if obj <=> min is less than zero or if anObject <=> max is greater than zero, true otherwise.

3.between?(1, 5)               #=> true
6.between?(1, 5)               #=> false
'cat'.between?('ant', 'dog')   #=> true
'gnu'.between?('ant', 'dog')   #=> false

Returns:

  • (Boolean)


# File 'compar.c'

static VALUE
cmp_between(x, min, max)
    VALUE x, min, max;
{
    if (RTEST(cmp_lt(x, min))) return Qfalse;
    if (RTEST(cmp_gt(x, max))) return Qfalse;
    return Qtrue;
}