Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
IO::Buffer#<=>
(::IO::Buffer) -> ::Integer
<=>(other) → integer
io_buffer.c
View on GitHub ↗
static VALUE
rb_io_buffer_compare(VALUE self, VALUE other)
{
const void *ptr1, *ptr2;
size_t size1, size2;
rb_io_buffer_get_bytes_for_reading(self, &ptr1, &size1);
rb_io_buffer_get_bytes_for_reading(other, &ptr2, &size2);
if (size1 < size2) {
return RB_INT2NUM(-1);
}
if (size1 > size2) {
return RB_INT2NUM(1);
}
if (size1 == 0) {
return RB_INT2NUM(0);
}
RUBY_ASSERT(ptr1 != NULL);
RUBY_ASSERT(ptr2 != NULL);
return RB_INT2NUM(memcmp(ptr1, ptr2, size1));
}
Returns a negative integer, zero, or a positive integer if the receiver is less than, equal to, or greater than other, respectively.
Buffers are compared by size first, and if the sizes are equal, by the exact contents of the memory they are referencing using memcmp. Only the sign of the returned integer is meaningful; the result of memcmp is returned as is.
Example
IO::Buffer.for("abc") <=> IO::Buffer.for("abc") # => 0 IO::Buffer.for("abc") <=> IO::Buffer.for("ab") # => 1 IO::Buffer.for("abc") <=> IO::Buffer.for("abd") # => -1