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#source

source → io_buffer, string, or nil

Returns the object backing this view, or nil for a source-less buffer. A slice’s source is the buffer on which slice was called, including when that buffer is itself a slice. The source is retained while the view lives.

Example
root = IO::Buffer.new(8)
parent = root.slice(1, 6)
child = parent.slice(1, 2)
child.source.equal?(parent) # => true
parent.source.equal?(root)  # => true
root.source                 # => nil

For a String-backed buffer the source is its backing String. Without a block, IO::Buffer.for may use a frozen internal copy rather than the original String. A source-less buffer may own or borrow its memory; a nil source does not imply allocation ownership.

An invalid slice still retains and returns its source. Storage#free or Storage#transfer clears that Storage object’s source; Slice has neither operation. There is no source setter.