Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
IO#each_byte
() { (::Integer byte) -> void } -> self
() -> ::Enumerator[::Integer, self]
each_byte {|byte| ... } → self
each_byte → enumerator
io.c
View on GitHub ↗
static VALUE
rb_io_each_byte(VALUE io)
{
rb_io_t *fptr;
RETURN_ENUMERATOR(io, 0, 0);
GetOpenFile(io, fptr);
do {
while (fptr->rbuf.len > 0) {
char *p = fptr->rbuf.ptr + fptr->rbuf.off++;
fptr->rbuf.len--;
rb_yield(INT2FIX(*p & 0xff));
rb_io_check_byte_readable(fptr);
errno = 0;
}
READ_CHECK(fptr);
} while (io_fillbuf(fptr) >= 0);
return io;
}
Calls the given block with each byte (0..255) in the stream; returns self. See Byte IO.
Example
File.read('t.ja') # => "こんにちは" f = File.new('t.ja') a = [] f.each_byte {|b| a << b } a # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175] f.close
Returns an Enumerator if no block is given.
Related: IO#each_char, IO#each_codepoint.