Skip to content

Commit 6628d48

Browse files
BurdetteLamarkou
andauthored
[DOC] Doc for StringIO#each_codepoint (#159)
Co-authored-by: Sutou Kouhei <[email protected]>
1 parent ec6bf81 commit 6628d48

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

ext/stringio/stringio.c

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,10 +1250,44 @@ strio_each_char(VALUE self)
12501250
* call-seq:
12511251
* each_codepoint {|codepoint| ... } -> self
12521252
*
1253-
* With a block given, calls the block with each remaining codepoint in the stream;
1254-
* see {Codepoint IO}[rdoc-ref:IO@Codepoint+IO].
1253+
* With a block given, calls the block with each successive codepoint from self;
1254+
* sets the position to end-of-stream;
1255+
* returns +self+.
12551256
*
1256-
* With no block given, returns an enumerator.
1257+
* Each codepoint is the integer value for a character; returns self:
1258+
*
1259+
* codepoints = []
1260+
* strio = StringIO.new('hello')
1261+
* strio.each_codepoint {|codepoint| codepoints.push(codepoint) }
1262+
* strio.eof? # => true
1263+
* codepoints # => [104, 101, 108, 108, 111]
1264+
* codepoints = []
1265+
* strio = StringIO.new('тест')
1266+
* strio.each_codepoint {|codepoint| codepoints.push(codepoint) }
1267+
* codepoints # => [1090, 1077, 1089, 1090]
1268+
* codepoints = []
1269+
* strio = StringIO.new('こんにちは')
1270+
* strio.each_codepoint {|codepoint| codepoints.push(codepoint) }
1271+
* codepoints # => [12371, 12435, 12395, 12385, 12399]
1272+
*
1273+
* Position in the stream matters:
1274+
*
1275+
* codepoints = []
1276+
* strio = StringIO.new('こんにちは')
1277+
* strio.getc # => "こ"
1278+
* strio.pos # => 3
1279+
* strio.each_codepoint {|codepoint| codepoints.push(codepoint) }
1280+
* codepoints # => [12435, 12395, 12385, 12399]
1281+
*
1282+
* When at end-of-stream, the block is not called:
1283+
*
1284+
* strio.eof? # => true
1285+
* strio.each_codepoint {|codepoint| fail 'Boo!' }
1286+
* strio.eof? # => true
1287+
*
1288+
* With no block given, returns a new {Enumerator}[https://docs.ruby-lang.org/en/master/Enumerator.html].
1289+
*
1290+
* Related: StringIO#each_byte, StringIO#each_char, StringIO#each_line.
12571291
*/
12581292
static VALUE
12591293
strio_each_codepoint(VALUE self)

0 commit comments

Comments
 (0)