instance method
StringIO#each_codepoint
() { (::Integer arg0) -> untyped } -> self
() -> ::Enumerator[::Integer, self]
each_codepoint {|codepoint| ... } → self
ext/stringio/stringio.c
View on GitHub ↗
static VALUE
strio_each_codepoint(VALUE self)
{
struct StringIO *ptr;
rb_encoding *enc;
unsigned int c;
int n;
RETURN_ENUMERATOR(self, 0, 0);
ptr = readable(self);
enc = get_enc(ptr);
while ((ptr = strio_to_read(self)) != NULL) {
c = rb_enc_codepoint_len(RSTRING_PTR(ptr->string)+ptr->pos,
RSTRING_END(ptr->string), &n, enc);
ptr->pos += n;
rb_yield(UINT2NUM(c));
}
return self;
}
With a block given, calls the block with each successive codepoint from self; sets the position to end-of-stream; returns self.
Each codepoint is the integer value for a character; returns self:
Example
codepoints = [] strio = StringIO.new('hello') strio.each_codepoint {|codepoint| codepoints.push(codepoint) } strio.eof? # => true codepoints # => [104, 101, 108, 108, 111] codepoints = [] strio = StringIO.new('тест') strio.each_codepoint {|codepoint| codepoints.push(codepoint) } codepoints # => [1090, 1077, 1089, 1090] codepoints = [] strio = StringIO.new('こんにちは') strio.each_codepoint {|codepoint| codepoints.push(codepoint) } codepoints # => [12371, 12435, 12395, 12385, 12399]
Position in the stream matters:
Example
codepoints = [] strio = StringIO.new('こんにちは') strio.getc # => "こ" strio.pos # => 3 strio.each_codepoint {|codepoint| codepoints.push(codepoint) } codepoints # => [12435, 12395, 12385, 12399]
When at end-of-stream, the block is not called:
Example
strio.eof? # => true strio.each_codepoint {|codepoint| fail 'Boo!' } strio.eof? # => true
With no block given, returns a new Enumerator.
Related: StringIO#each_byte, StringIO#each_char, StringIO#each_line.