bytes()
public
Calls the given block once for each byte
(0..255) in ios, passing the byte as an argument. The stream must
be opened for reading or an IOError will be
raised.
If no block is given, an enumerator is returned instead.
f = File.new("testfile")
checksum = 0
f.each_byte {|x| checksum ^= x }
checksum
Show source
static VALUE
rb_io_each_byte(VALUE io)
{
rb_io_t *fptr;
char *p, *e;
RETURN_ENUMERATOR(io, 0, 0);
GetOpenFile(io, fptr);
for (;;) {
while (fptr->rbuf.len > 0) {
p = fptr->rbuf.ptr + fptr->rbuf.off++;
e = p + fptr->rbuf.len--;
rb_yield(INT2FIX(*p & 0xff));
errno = 0;
}
rb_io_check_byte_readable(fptr);
READ_CHECK(fptr);
if (io_fillbuf(fptr) < 0) {
break;
}
}
return io;
}