Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
IO::Buffer::Slice#initialize_copy
dup → slice
clone → slice
io_buffer.c
View on GitHub ↗
static VALUE
io_buffer_slice_initialize_copy(VALUE self, VALUE other)
{
if (self == other) return self;
rb_check_frozen(self);
struct rb_io_buffer_view *slice = get_io_buffer_view(self);
const struct rb_io_buffer_view *original = get_io_buffer_view(other);
if (!io_buffer_slice_p(original)) rb_raise(rb_eTypeError, "Expected IO::Buffer::Slice!");
if (!NIL_P(slice->source)) rb_raise(rb_eRuntimeError, "Slice is already initialized!");
slice->base = original->base;
slice->size = original->size;
slice->flags = original->flags;
RB_OBJ_WRITE(self, &slice->source, original->source);
return self;
}
Copies the view, retaining the same source, offset, and size without copying the underlying bytes. Advancing or resizing the copy does not change the original view; writes through either view affect shared storage.
Example
buffer = IO::Buffer.new(6) buffer.set_string("abcdef") slice = buffer.slice(1, 3) copy = slice.dup copy.source.equal?(slice.source) # => true copy.advance(1) copy.get_string # => "cd" slice.get_string # => "bcd" copy.set_string("XY") buffer.get_string # => "abXYef"
This differs from IO::Buffer::Storage#dup, which copies the bytes into independent storage. The usual Object#dup and Object#clone frozen-state rules apply.