End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
IO::Buffer#get_string
get_string([offset, [length, [encoding]]]) → string
io_buffer.c
View on GitHub ↗
static VALUE
io_buffer_get_string(int argc, VALUE *argv, VALUE self)
{
if (argc > 3) rb_error_arity(argc, 0, 3);
struct rb_io_buffer *data = NULL;
TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, data);
const void *base;
size_t size;
io_buffer_get_bytes_for_reading(data, &base, &size);
size_t offset = 0;
size_t length = size;
rb_encoding *encoding = rb_ascii8bit_encoding();
if (argc >= 1) {
offset = NUM2SIZET(argv[0]);
}
if (argc >= 2 && !RB_NIL_P(argv[1])) {
length = NUM2SIZET(argv[1]);
} else {
length = size - offset;
}
if (argc >= 3) {
encoding = rb_find_encoding(argv[2]);
}
io_buffer_validate_range(data, offset, length);
return rb_enc_str_new((const char*)base + offset, length, encoding);
}
Read a chunk or all of the buffer into a string, in the specified encoding. If no encoding is provided Encoding::BINARY is used.
Example
buffer = IO::Buffer.for('test') buffer.get_string # => "test" buffer.get_string(2) # => "st" buffer.get_string(2, 1) # => "s"