class method
IO::Buffer.string
(::int) { (::IO::Buffer) -> void } -> ::String
IO::Buffer.string(length) {|io_buffer| ... read/write io_buffer ...} → string
io_buffer.c
View on GitHub ↗
VALUE
rb_io_buffer_type_string(VALUE klass, VALUE length)
{
VALUE string = rb_str_new(NULL, RB_NUM2LONG(length));
struct io_buffer_for_yield_instance_arguments arguments = {
.klass = klass,
.string = string,
.instance = Qnil,
};
rb_ensure(io_buffer_for_yield_instance, (VALUE)&arguments, io_buffer_for_yield_instance_ensure, (VALUE)&arguments);
return string;
}
Creates a new string of the given length and yields a zero-copy IO::Buffer instance to the block which uses the string as a source. The block is expected to write to the buffer and the string will be returned.
Example
IO::Buffer.string(4) do |buffer| buffer.set_string("Ruby") end # => "Ruby"