mirror of
https://github.com/ruby/ruby.git
synced 2026-01-27 12:34:21 +00:00
C99 does not declare ways to designate the charset encoding of the source file. We can assume just US-ASCII characters will be safe.
35 lines
1.0 KiB
Plaintext
35 lines
1.0 KiB
Plaintext
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].
|