ruby/doc/stringio/each_char.rdoc
Nobuyoshi Nakada 0f5c69b317 [DOC] Moved non ASCII documents to separated files
C99 does not declare ways to designate the charset encoding of the
source file.  We can assume just US-ASCII characters will be safe.
2025-10-28 13:07:59 +09:00

35 lines
1.0 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

With a block given, calls the block with each remaining character in the stream;
positions the stream at end-of-file;
returns +self+:
chars = []
strio = StringIO.new('hello')
strio.each_char {|char| chars.push(char) }
strio.eof? # => true
chars # => ["h", "e", "l", "l", "o"]
chars = []
strio = StringIO.new('тест')
strio.each_char {|char| chars.push(char) }
chars # => ["т", "е", "с", "т"]
chars = []
strio = StringIO.new('こんにちは')
strio.each_char {|char| chars.push(char) }
chars # => ["こ", "ん", "に", "ち", "は"]
Stream position matters:
chars = []
strio = StringIO.new('こんにちは')
strio.getc # => "こ"
strio.pos # => 3 # 3-byte character was read.
strio.each_char {|char| chars.push(char) }
chars # => ["ん", "に", "ち", "は"]
When at end-of-stream does not call the block:
strio.eof? # => true
strio.each_char {|char| fail 'Boo!' }
strio.eof? # => true
With no block given, returns a new {Enumerator}[rdoc-ref:Enumerator].