Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

IO::Buffer::Storage.new

IO::Buffer.new([size = DEFAULT_SIZE, [flags]]) → io_buffer

Create a new zero-filled IO::Buffer of size bytes. By default, the buffer will be internal: directly allocated chunk of the memory. But if the requested size is more than OS-specific IO::Buffer::PAGE_SIZE, the buffer would be allocated using the virtual memory mechanism (anonymous mmap on Unix, VirtualAlloc on Windows). The behavior can be forced by passing IO::Buffer::MAPPED as a second parameter.

IO::Buffer::SHARED and IO::Buffer::PRIVATE imply IO::Buffer::MAPPED and are mutually exclusive. Otherwise, if flags do not include an allocation mode, IO::Buffer::INTERNAL or IO::Buffer::MAPPED is inferred from the requested size. The two allocation modes are mutually exclusive.

Example
buffer = IO::Buffer.new(4)
# =>
# #<IO::Buffer 0x000055b34497ea10+4 INTERNAL>
# 0x00000000  00 00 00 00                                     ....

buffer.get_string(0, 1) # => "\x00"

buffer.set_string("test")
buffer
# =>
# #<IO::Buffer 0x000055b34497ea10+4 INTERNAL>
# 0x00000000  74 65 73 74                                     test
See also #external? #free