Files
ruby/doc/stringio/each_char.rdoc

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].