summaryrefslogtreecommitdiff
path: root/doc/stringio/each_char.rdoc
blob: d0b5e4082cc4d37864e621b5d0849617a201a7ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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].