Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
StringScanner#getch
() -> ::String?
getch
ext/strscan/strscan.c
View on GitHub ↗
static VALUE
strscan_getch(VALUE self)
{
struct strscanner *p;
long len;
GET_SCANNER(self, p);
CLEAR_MATCH_STATUS(p);
if (EOS_P(p))
return Qnil;
len = rb_enc_mbclen(CURPTR(p), S_PEND(p), rb_enc_get(p->str));
len = minl(len, S_RESTLEN(p));
p->prev = p->curr;
p->curr += len;
MATCHED(p);
adjust_registers_to_matched(p);
return extract_range(p,
adjust_register_position(p, p->regs.beg[0]),
adjust_register_position(p, p->regs.end[0]));
}
call-seq: getch -> character or nil
Returns the next (possibly multibyte) character, if available:
-
If the position is at the beginning of a character:
-
Returns the character.
-
Increments the character position by 1.
-
Increments the byte position by the size (in bytes) of the character.
Examplescanner = StringScanner.new(HIRAGANA_TEXT) scanner.string # => "こんにちは" [scanner.getch, scanner.pos, scanner.charpos] # => ["こ", 3, 1] [scanner.getch, scanner.pos, scanner.charpos] # => ["ん", 6, 2] [scanner.getch, scanner.pos, scanner.charpos] # => ["に", 9, 3] [scanner.getch, scanner.pos, scanner.charpos] # => ["ち", 12, 4] [scanner.getch, scanner.pos, scanner.charpos] # => ["は", 15, 5] [scanner.getch, scanner.pos, scanner.charpos] # => [nil, 15, 5]
-
-
If the position is within a multi-byte character (that is, not at its beginning), behaves like
get_byte(returns a 1-byte character):Examplescanner.pos = 1 [scanner.getch, scanner.pos, scanner.charpos] # => ["\x81", 2, 2] [scanner.getch, scanner.pos, scanner.charpos] # => ["\x93", 3, 1] [scanner.getch, scanner.pos, scanner.charpos] # => ["ん", 6, 2]
-
If the position is at the end of the stored string, returns
niland does not modify the positions:Examplescanner.terminate [scanner.getch, scanner.pos, scanner.charpos] # => [nil, 15, 5]
See also
#get_byte
#initialize_copy
#fixed_anchor?
#inspect
also in
4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0