Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
StringIO#getbyte
() -> ::Integer?
getbyte → integer or nil
ext/stringio/stringio.c
View on GitHub ↗
static VALUE
strio_getbyte(VALUE self)
{
struct StringIO *ptr = readable(self);
int c;
if (eos_p(ptr)) {
return Qnil;
}
c = RSTRING_PTR(ptr->string)[ptr->pos++];
return CHR2FIX(c);
}
Reads and returns the next integer byte (not character) from the stream:
Example
s = 'foo' s.bytes # => [102, 111, 111] strio = StringIO.new(s) strio.getbyte # => 102 strio.getbyte # => 111 strio.getbyte # => 111
Returns nil if at end-of-stream:
Example
strio.eof? # => true strio.getbyte # => nil
Returns a byte, not a character:
Example
s = 'こんにちは' s.bytes # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175] strio = StringIO.new(s) strio.getbyte # => 227 strio.getbyte # => 129