Skip to content

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

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.

See also .new #resize