mirror of
https://github.com/ruby/ruby.git
synced 2026-10-28 16:36:20 +08:00
(https://github.com/ruby/stringio/pull/201) https://github.com/ruby/stringio/commit/26d8f60053
32 lines
895 B
Plaintext
32 lines
895 B
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 # => ["こ", "ん", "に", "ち", "は"]
|
|
|
|
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].
|