mirror of
https://github.com/ruby/ruby.git
synced 2026-08-16 02:15:57 +08:00
(https://github.com/ruby/stringio/pull/201) https://github.com/ruby/stringio/commit/26d8f60053
32 lines
1.0 KiB
Plaintext
32 lines
1.0 KiB
Plaintext
With a block given, calls the block with each remaining byte in the stream;
|
|
positions the stream at end-of-file;
|
|
returns +self+:
|
|
|
|
bytes = []
|
|
strio = StringIO.new('hello') # Five 1-byte characters.
|
|
strio.each_byte {|byte| bytes.push(byte) }
|
|
strio.eof? # => true
|
|
bytes # => [104, 101, 108, 108, 111]
|
|
|
|
bytes = []
|
|
strio = StringIO.new('こんにちは') # Five 3-byte characters.
|
|
strio.each_byte {|byte| bytes.push(byte) }
|
|
bytes # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
|
|
|
|
The position in the stream matters:
|
|
|
|
bytes = []
|
|
strio = StringIO.new('こんにちは')
|
|
strio.getc # => "こ"
|
|
strio.pos # => 3 # 3-byte character was read.
|
|
strio.each_byte {|byte| bytes.push(byte) }
|
|
bytes # => [227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
|
|
|
|
If at end-of-file, does not call the block:
|
|
|
|
strio.eof? # => true
|
|
strio.each_byte {|byte| fail 'Boo!' }
|
|
strio.eof? # => true
|
|
|
|
With no block given, returns a new {Enumerator}[rdoc-ref:Enumerator].
|