[DOC] More on IO streams (#6454)

Adds remarks about .new and .open.
Uses ..open where convenient (not convenient where output would be in a block).
Fixed examples for #ungetc.
This commit is contained in:
Burdette Lamar 2022-09-27 13:58:28 -05:00 committed by GitHub
parent bcd30fb961
commit 5d4048e0bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
Notes: git 2022-09-28 03:58:51 +09:00
Merged-By: BurdetteLamar <BurdetteLamar@Yahoo.com>

View File

@ -53,22 +53,29 @@ You can create streams:
- \File: - \File:
- File.new: returns a new \File object. - File.new: returns a new \File object;
- File.open: passes a new \File object to given the block. the file should be closed when no longer needed.
- File.open: passes a new \File object to given the block;
the file is automatically closed on block exit.
- \IO: - \IO:
- IO.new: returns a new \IO object for the given integer file descriptor. - IO.new: returns a new \IO object for the given integer file descriptor;
- IO.open: passes a new \IO object to the given block. the \IO object should be closed when no longer needed.
- IO.open: passes a new \IO object to the given block;
the \IO object is automatically closed on block exit.
- IO.popen: returns a new \IO object that is connected to the $stdin - IO.popen: returns a new \IO object that is connected to the $stdin
and $stdout of a newly-launched subprocess. and $stdout of a newly-launched subprocess.
- Kernel#open: returns a new \IO object connected to a given source: - Kernel#open: returns a new \IO object connected to a given source:
stream, file, or subprocess. stream, file, or subprocess;
the \IO object should be closed when no longer needed.
- \StringIO: - \StringIO:
- StringIO.new: returns a new \StringIO object. - StringIO.new: returns a new \StringIO object;
- StringIO.open: passes a new \StringIO object to the given block. the \StringIO object should be closed when no longer needed.
- StringIO.open: passes a new \StringIO object to the given block;
the \StringIO object is automatically closed on block exit.
(You cannot create an \ARGF object, but one already exists.) (You cannot create an \ARGF object, but one already exists.)
@ -375,11 +382,11 @@ Reading lines from a stream usually changes its line number:
Iterating over lines in a stream usually changes its line number: Iterating over lines in a stream usually changes its line number:
f = File.new('t.txt') File.open('t.txt') do |f|
f.each_line do |line| f.each_line do |line|
p "position=#{f.pos} eof?=#{f.eof?} lineno=#{f.lineno}" p "position=#{f.pos} eof?=#{f.eof?} lineno=#{f.lineno}"
end
end end
f.close
Output: Output:
@ -417,27 +424,30 @@ You can process an \IO stream character-by-character using these methods:
- IO#ungetc (not in \ARGF): - IO#ungetc (not in \ARGF):
Pushes back ("unshifts") a character or integer onto the stream: Pushes back ("unshifts") a character or integer onto the stream:
f = File.new('t.tmp', 'w') path = 't.tmp'
f.putc("т") File.write(path, 'foo')
f.putc("т") File.open(path) do |f|
f.close f.ungetc('т')
File.read('t.tmp') # => "тт" f.read # => "тfoo"
end
- IO#putc (also in Kernel): Writes a character to the stream: - IO#putc (also in Kernel): Writes a character to the stream:
c = File.new('t.rus').getc # => "т" File.open('t.tmp', 'w') do |f|
f = File.new('t.tmp', 'w') f.putc('т')
f.putc(c) f.putc('е')
f.putc(c) f.putc('с')
f.close f.putc('т')
File.read('t.tmp') # => "тт" end
File.read('t.tmp') # => "тест"
- IO#each_char: Reads each remaining character in the stream, - IO#each_char: Reads each remaining character in the stream,
passing the character to the given block: passing the character to the given block:
f = File.new('t.rus') File.open('t.rus') do |f|
f.pos = 4 f.pos = 4
f.each_char {|c| p c } f.each_char {|c| p c }
end
Output: Output:
@ -492,8 +502,8 @@ You can process an \IO stream byte-by-byte using these methods:
You can process an \IO stream codepoint-by-codepoint using method You can process an \IO stream codepoint-by-codepoint using method
+#each_codepoint+: +#each_codepoint+:
f = File.new('t.rus')
a = [] a = []
f.each_codepoint {|c| a << c } File.open('t.rus') do |f|
f.each_codepoint {|c| a << c }
end
a # => [1090, 1077, 1089, 1090] a # => [1090, 1077, 1089, 1090]
f.close