Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Module#<=>
(untyped other) -> (-1 | 0 | 1)?
self <=> other → -1, 0, 1, or nil
object.c
View on GitHub ↗
static VALUE
rb_mod_cmp(VALUE mod, VALUE arg)
{
VALUE cmp;
if (mod == arg) return INT2FIX(0);
if (!CLASS_OR_MODULE_P(arg)) {
return Qnil;
}
cmp = rb_class_inherited_p(mod, arg);
if (NIL_P(cmp)) return Qnil;
if (cmp) {
return INT2FIX(-1);
}
return INT2FIX(1);
}
Compares self and other.
Returns:
-
-1, ifselfincludesother, if orselfis a subclass ofother. -
0, ifselfandotherare the same. -
1, ifotherincludesself, or ifotheris a subclass ofself. -
nil, if none of the above is true.
Examples:
Example
# Class Array includes module Enumerable. Array <=> Enumerable # => -1 Enumerable <=> Enumerable # => 0 Enumerable <=> Array # => 1 # Class File is a subclass of class IO. File <=> IO # => -1 File <=> File # => 0 IO <=> File # => 1 # Class File has no relationship to class String. File <=> String # => nil