End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
IO::Buffer.new
IO::Buffer.new([size = DEFAULT_SIZE, [flags = 0]]) → io_buffer
io_buffer.c
View on GitHub ↗
VALUE
rb_io_buffer_initialize(int argc, VALUE *argv, VALUE self)
{
io_buffer_experimental();
if (argc < 0 || argc > 2) {
rb_error_arity(argc, 0, 2);
}
struct rb_io_buffer *data = NULL;
TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, data);
size_t size;
if (argc > 0) {
size = RB_NUM2SIZE(argv[0]);
} else {
size = RUBY_IO_BUFFER_DEFAULT_SIZE;
}
enum rb_io_buffer_flags flags = 0;
if (argc >= 2) {
flags = RB_NUM2UINT(argv[1]);
}
else {
flags |= io_flags_for_size(size);
}
io_buffer_initialize(data, NULL, size, flags, Qnil);
return self;
}
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::Bufer::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.
Examples
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