End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
IO::Buffer#each_byte
each_byte([offset, [count]]) {|offset, byte| ...} → self
each_byte([offset, [count]]) → enumerator
io_buffer.c
View on GitHub ↗
static VALUE
io_buffer_each_byte(int argc, VALUE *argv, VALUE self)
{
RETURN_ENUMERATOR_KW(self, argc, argv, RB_NO_KEYWORDS);
const void *base;
size_t size;
rb_io_buffer_get_bytes_for_reading(self, &base, &size);
size_t offset;
if (argc >= 2) {
offset = NUM2SIZET(argv[1]);
} else {
offset = 0;
}
size_t count;
if (argc >= 3) {
count = NUM2SIZET(argv[2]);
} else {
count = (size - offset);
}
for (size_t i = 0; i < count; i++) {
unsigned char *value = (unsigned char *)base + i + offset;
rb_yield(RB_INT2FIX(*value));
}
return self;
}
Iterates over the buffer, yielding each byte starting from offset.
If count is given, only count bytes will be yielded.
Example:
Example
IO::Buffer.for("Hello World").each_byte(2, 2) do |offset, byte| puts "#{offset}: #{byte}" end # 2: 108 # 3: 108