Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Pathname#<=>
(::Pathname other) -> ::Integer
(untyped other) -> nil
self <=> other → -1, 0, 1, or nil
pathname.c
View on GitHub ↗
static VALUE
path_cmp(VALUE self, VALUE other)
{
VALUE s1, s2;
char *p1, *p2;
char *e1, *e2;
if (!rb_obj_is_kind_of(other, rb_cPathname))
return Qnil;
s1 = get_strpath(self);
s2 = get_strpath(other);
p1 = RSTRING_PTR(s1);
p2 = RSTRING_PTR(s2);
e1 = p1 + RSTRING_LEN(s1);
e2 = p2 + RSTRING_LEN(s2);
while (p1 < e1 && p2 < e2) {
int c1, c2;
c1 = (unsigned char)*p1++;
c2 = (unsigned char)*p2++;
if (c1 == '/') c1 = '\0';
if (c2 == '/') c2 = '\0';
if (c1 != c2) {
if (c1 < c2)
return INT2FIX(-1);
else
return INT2FIX(1);
}
}
if (p1 < e1)
return INT2FIX(1);
if (p2 < e2)
return INT2FIX(-1);
return INT2FIX(0);
}
Compares the contents of self and other as strings; see String#<=>.
Returns:
-
-1ifself‘s string is smaller thanother’s string. -
0if the two are equal. -
1ifself‘s string is larger thanother’s string. -
nilifotheris not a Pathname.
Examples:
Example
Pathname('a') <=> Pathname('b') # => -1 Pathname('a') <=> Pathname('ab') # => -1 Pathname('a') <=> Pathname('a') # => 0 Pathname('b') <=> Pathname('a') # => 1 Pathname('ab') <=> Pathname('a') # => 1 Pathname('ab') <=> 'a' # => nil
Two pathnames that are different may refer to the same entry in the filesystem:
Example
Pathname('lib') <=> Pathname('./lib') # => 1