summaryrefslogtreecommitdiff
path: root/doc/stringio/getc.rdoc
blob: b2ab46843c8466db1d17215708b23951009e24ec (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
Reads and returns the next character (or byte; see below) from the stream:

  strio = StringIO.new('foo')
  strio.getc # => "f"
  strio.getc # => "o"
  strio.getc # => "o"

Returns +nil+ if at end-of-stream:

  strio.eof? # => true
  strio.getc # => nil

Returns characters, not bytes:

  strio = StringIO.new('Привет')
  strio.getc # => "П"
  strio.getc # => "р"

  strio = StringIO.new('こんにちは')
  strio.getc # => "こ"
  strio.getc # => "ん"

In each of the examples above, the stream is positioned at the beginning of a character;
in other cases that need not be true:

   strio = StringIO.new('こんにちは')  # Five 3-byte characters.
   strio.pos = 3 # => 3     # At beginning of second character; returns character.
   strio.getc    # => "ん"
   strio.pos = 4 # => 4     # At second byte of second character; returns byte.
   strio.getc    # => "\x82"
   strio.pos = 5 # => 5     # At third byte of second character; returns byte.
   strio.getc    # => "\x93"

Related: #getbyte, #putc, #ungetc.