each_byte()
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.
f = File.new("testfile")
checksum = 0
f.each_byte {|x| checksum ^= x }
checksum
Show source
/*
* call-seq:
* ios.each_byte {|byte| block } => ios
*
* Calls the given block once for each byte (0..255) in <em>ios</em>,
* passing the byte as an argument. The stream must be opened for
* reading or an <code>IOError</code> will be raised.
*
* f = File.new("testfile")
* checksum = 0
* f.each_byte {|x| checksum ^= x } #=> #<File:testfile>
* checksum #=> 12
*/
static VALUE
rb_io_each_byte(io)
VALUE io;
{
rb_io_t *fptr;
FILE *f;
int c;
RETURN_ENUMERATOR(io, 0, 0);
GetOpenFile(io, fptr);
for (;;) {
rb_io_check_readable(fptr);
f = fptr->f;
READ_CHECK(f);
clearerr(f);
TRAP_BEG;
c = getc(f);
TRAP_END;
if (c == EOF) {
if (ferror(f)) {
clearerr(f);
if (!rb_io_wait_readable(fileno(f)))
rb_sys_fail(fptr->path);
continue;
}
break;
}
rb_yield(INT2FIX(c & 0xff));
}
if (ferror(f)) rb_sys_fail(fptr->path);
return io;
}