Skip to content
class method

IO::Buffer.new

(?::Integer size, ?::Integer flags) -> ::IO::Buffer
IO::Buffer.new([size = DEFAULT_SIZE, [flags = 0]]) → 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.

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 .map .size_of .for .string also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1