Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
IO#getbyte
() -> ::Integer?
getbyte → integer or nil
io.c
View on GitHub ↗
VALUE
rb_io_getbyte(VALUE io)
{
rb_io_t *fptr;
int c;
GetOpenFile(io, fptr);
rb_io_check_byte_readable(fptr);
READ_CHECK(fptr);
VALUE r_stdout = rb_ractor_stdout();
if (fptr->fd == 0 && (fptr->mode & FMODE_TTY) && RB_TYPE_P(r_stdout, T_FILE)) {
rb_io_t *ofp;
GetOpenFile(r_stdout, ofp);
if (ofp->mode & FMODE_TTY) {
rb_io_flush(r_stdout);
}
}
if (io_fillbuf(fptr) < 0) {
return Qnil;
}
fptr->rbuf.off++;
fptr->rbuf.len--;
c = (unsigned char)fptr->rbuf.ptr[fptr->rbuf.off-1];
return INT2FIX(c & 0xff);
}
Reads and returns the next byte (in range 0..255) from the stream; returns nil if already at end-of-stream. See Byte IO.
Example
f = File.open('t.txt') f.getbyte # => 70 f.close File.read('t.ja') # => "こんにちは" f = File.open('t.ja') f.getbyte # => 227 f.close
Related: IO#readbyte (may raise EOFError).