Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
IO::Buffer#get_string
(?::Integer offset, ?::Integer length, ?::Encoding encoding) -> ::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)
{
rb_check_arity(argc, 0, 3);
struct io_buffer_get_string_arguments arguments;
io_buffer_extract_offset_length(self, argc, argv, &arguments.offset, &arguments.length);
// Encoding coercion may invoke Ruby and change the source. Do it before
// locking and resolving the current view; retain no pointer across it.
arguments.encoding = argc >= 3 ? rb_find_encoding(argv[2]) : rb_ascii8bit_encoding();
return rb_io_buffer_locked_for_reading(self, io_buffer_get_string_locked, (VALUE)&arguments);
}
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"