mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
parent
21fa0135a4
commit
841be6392e
Notes:
git
2022-10-24 15:38:39 +00:00
Merged-By: BurdetteLamar <BurdetteLamar@Yahoo.com>
1 changed files with 188 additions and 260 deletions
|
@ -87,11 +87,10 @@ Many examples here use these variables:
|
|||
|
||||
=== Basic \IO
|
||||
|
||||
==== Reading and Writing
|
||||
You can perform basic stream \IO with these methods:
|
||||
|
||||
===== \Method <tt>#read</tt>
|
||||
|
||||
Returns all remaining or the next +n+ bytes read from the stream, for a given +n+:
|
||||
- IO#read: Returns all remaining or the next _n_ bytes read from the stream,
|
||||
for a given _n_:
|
||||
|
||||
f = File.new('t.txt')
|
||||
f.read # => "First line\nSecond line\n\nFourth line\nFifth line\n"
|
||||
|
@ -101,14 +100,12 @@ Returns all remaining or the next +n+ bytes read from the stream, for a given +n
|
|||
f.read(30) # => nil
|
||||
f.close
|
||||
|
||||
===== \Method <tt>#write</tt>
|
||||
|
||||
Writes one or more given strings to the stream:
|
||||
- IO#write: Writes one or more given strings to the stream:
|
||||
|
||||
$stdout.write('Hello', ', ', 'World!', "\n") # => 14
|
||||
$stdout.write('foo', :bar, 2, "\n")
|
||||
|
||||
Output:
|
||||
Output:
|
||||
|
||||
Hello, World!
|
||||
foobar2
|
||||
|
@ -120,9 +117,10 @@ which is the byte offset at which the next read or write is to occur.
|
|||
A new stream has position zero (and line number zero);
|
||||
method +rewind+ resets the position (and line number) to zero.
|
||||
|
||||
===== \Method <tt>#tell</tt>
|
||||
The relevant methods:
|
||||
|
||||
Returns the current position (in bytes) in the stream:
|
||||
- IO#tell (aliased as +#pos+):
|
||||
Returns the current position (in bytes) in the stream:
|
||||
|
||||
f = File.new('t.txt')
|
||||
f.tell # => 0
|
||||
|
@ -130,11 +128,7 @@ Returns the current position (in bytes) in the stream:
|
|||
f.tell # => 12
|
||||
f.close
|
||||
|
||||
Aliased as <tt>pos</tt>.
|
||||
|
||||
===== \Method <tt>#pos=</tt>
|
||||
|
||||
Sets the position of the stream (in bytes):
|
||||
- IO#pos=: Sets the position of the stream (in bytes):
|
||||
|
||||
f = File.new('t.txt')
|
||||
f.tell # => 0
|
||||
|
@ -142,12 +136,10 @@ Sets the position of the stream (in bytes):
|
|||
f.tell # => 20
|
||||
f.close
|
||||
|
||||
===== \Method <tt>#seek</tt>
|
||||
- IO#seek: Sets the position of the stream to a given integer +offset+
|
||||
(in bytes), with respect to a given constant +whence+, which is one of:
|
||||
|
||||
Sets the position of the stream to a given integer +offset+
|
||||
(in bytes), with respect to a given constant +whence+, which is one of:
|
||||
|
||||
- +:CUR+ or <tt>IO::SEEK_CUR</tt>:
|
||||
- +:CUR+ or <tt>IO::SEEK_CUR</tt>:
|
||||
Repositions the stream to its current position plus the given +offset+:
|
||||
|
||||
f = File.new('t.txt')
|
||||
|
@ -158,7 +150,7 @@ Sets the position of the stream to a given integer +offset+
|
|||
f.tell # => 10
|
||||
f.close
|
||||
|
||||
- +:END+ or <tt>IO::SEEK_END</tt>:
|
||||
- +:END+ or <tt>IO::SEEK_END</tt>:
|
||||
Repositions the stream to its end plus the given +offset+:
|
||||
|
||||
f = File.new('t.txt')
|
||||
|
@ -171,7 +163,7 @@ Sets the position of the stream to a given integer +offset+
|
|||
f.tell # => 12
|
||||
f.close
|
||||
|
||||
- +:SET+ or <tt>IO:SEEK_SET</tt>:
|
||||
- +:SET+ or <tt>IO:SEEK_SET</tt>:
|
||||
Repositions the stream to the given +offset+:
|
||||
|
||||
f = File.new('t.txt')
|
||||
|
@ -182,9 +174,7 @@ Sets the position of the stream to a given integer +offset+
|
|||
f.tell # => 40
|
||||
f.close
|
||||
|
||||
===== \Method <tt>#rewind</tt>
|
||||
|
||||
Positions the stream to the beginning (also resetting the line number):
|
||||
- IO#rewind: Positions the stream to the beginning (also resetting the line number):
|
||||
|
||||
f = File.new('t.txt')
|
||||
f.tell # => 0
|
||||
|
@ -199,33 +189,20 @@ Positions the stream to the beginning (also resetting the line number):
|
|||
|
||||
A new \IO stream may be open for reading, open for writing, or both.
|
||||
|
||||
===== \Method <tt>#close</tt>
|
||||
You can close a stream using these methods:
|
||||
|
||||
Closes the stream for both reading and writing.
|
||||
- IO#close: Closes the stream for both reading and writing.
|
||||
- IO#close_read (not in \ARGF): Closes the stream for reading.
|
||||
- IO#close_write (not in \ARGF): Closes the stream for writing.
|
||||
|
||||
===== \Method <tt>#close_read</tt>
|
||||
You can query whether a stream is closed using this method:
|
||||
|
||||
Closes the stream for reading,
|
||||
|
||||
Not in ARGF.
|
||||
|
||||
===== \Method <tt>#close_write</tt>
|
||||
|
||||
Closes the stream for writing
|
||||
|
||||
Not in ARGF.
|
||||
|
||||
===== \Method <tt>#closed?</tt>
|
||||
|
||||
Returns whether the stream is closed.
|
||||
- IO#closed?: Returns whether the stream is closed.
|
||||
|
||||
==== End-of-Stream
|
||||
|
||||
===== \Method <tt>#eof?</tt>
|
||||
|
||||
Returns whether a stream is positioned at its end; aliased as +#eof+.
|
||||
|
||||
===== Repositioning to End-of-Stream
|
||||
You can query whether a stream is positioned at its end using
|
||||
method IO#eof? (also aliased as +#eof+).
|
||||
|
||||
You can reposition to end-of-stream by reading all stream content:
|
||||
|
||||
|
@ -243,16 +220,14 @@ Or by using method IO#seek:
|
|||
|
||||
=== Line \IO
|
||||
|
||||
You can process an \IO stream line-by-line.
|
||||
You can read an \IO stream line-by-line using these methods:
|
||||
|
||||
===== \Method <tt>#each_line</tt>
|
||||
|
||||
Passes each line to the block:
|
||||
- IO#each_line: Passes each line to the block:
|
||||
|
||||
f = File.new('t.txt')
|
||||
f.each_line {|line| p line }
|
||||
|
||||
Output:
|
||||
Output:
|
||||
|
||||
"First line\n"
|
||||
"Second line\n"
|
||||
|
@ -260,20 +235,18 @@ Output:
|
|||
"Fourth line\n"
|
||||
"Fifth line\n"
|
||||
|
||||
The reading may begin mid-line:
|
||||
The reading may begin mid-line:
|
||||
|
||||
f = File.new('t.txt')
|
||||
f.pos = 27
|
||||
f.each_line {|line| p line }
|
||||
|
||||
Output:
|
||||
Output:
|
||||
|
||||
"rth line\n"
|
||||
"Fifth line\n"
|
||||
|
||||
===== \Method <tt>#gets</tt>
|
||||
|
||||
Returns the next line (which may begin mid-line); also in Kernel:
|
||||
- IO#gets (also in Kernel): Returns the next line (which may begin mid-line):
|
||||
|
||||
f = File.new('t.txt')
|
||||
f.gets # => "First line\n"
|
||||
|
@ -283,43 +256,32 @@ Returns the next line (which may begin mid-line); also in Kernel:
|
|||
f.readlines # => ["Fifth line\n"]
|
||||
f.gets # => nil
|
||||
|
||||
===== \Method <tt>#readline</tt>
|
||||
- IO#readline (also in Kernel; not in StringIO):
|
||||
Like #gets, but raises an exception at end-of-stream.
|
||||
|
||||
Like #gets, but raises an exception at end-of-stream.
|
||||
|
||||
Also in Kernel; not in StringIO.
|
||||
|
||||
===== \Method <tt>#readlines</tt>
|
||||
|
||||
Returns all remaining lines in an array;
|
||||
may begin mid-line:
|
||||
- IO#readlines (also in Kernel): Returns all remaining lines in an array;
|
||||
may begin mid-line:
|
||||
|
||||
f = File.new('t.txt')
|
||||
f.pos = 19
|
||||
f.readlines # => ["ine\n", "\n", "Fourth line\n", "Fifth line\n"]
|
||||
f.readlines # => []
|
||||
|
||||
Also in Kernel.
|
||||
|
||||
===== Optional Reader Arguments
|
||||
|
||||
Each of these reader methods may be called with:
|
||||
|
||||
- An optional line separator, +sep+.
|
||||
- An optional line-size limit, +limit+.
|
||||
- Both +sep+ and +limit+.
|
||||
|
||||
===== \Method <tt>#puts</tt>
|
||||
You can write to an \IO stream line-by-line using this method:
|
||||
|
||||
Writes objects to the stream:
|
||||
- IO#puts (also in Kernel; not in \StringIO): Writes objects to the stream:
|
||||
|
||||
f = File.new('t.tmp', 'w')
|
||||
f.puts('foo', :bar, 1, 2.0, Complex(3, 0))
|
||||
f.flush
|
||||
File.read('t.tmp') # => "foo\nbar\n1\n2.0\n3+0i\n"
|
||||
|
||||
Also in Kernel; not in StringIO.
|
||||
|
||||
==== Line Separator
|
||||
|
||||
The default line separator is the given by the global variable <tt>$/</tt>,
|
||||
|
@ -406,21 +368,13 @@ which is the non-negative integer line number
|
|||
in the stream where the next read will occur.
|
||||
|
||||
The line number is the number of lines read by certain line-oriented methods
|
||||
({::foreach}[rdoc-ref:IO.foreach],
|
||||
{#each_line}[rdoc-ref:io_streams.rdoc@Method+-23each_line],
|
||||
{#gets}[rdoc-ref:io_streams.rdoc@Method+-23gets],
|
||||
{#readline}[rdoc-ref:io_streams.rdoc@Method+-23readline],
|
||||
{#readlines}[rdoc-ref:io_streams.rdoc@Method+-23readlines])
|
||||
(IO.foreach, IO#each_line, IO#gets, IO#readline, and IO#readlines)
|
||||
according to the given (or default) line separator +sep+.
|
||||
|
||||
A new stream is initially has line number zero (and position zero);
|
||||
method +rewind+ resets the line number (and position) to zero.
|
||||
|
||||
===== \Method <tt>#lineno</tt>
|
||||
|
||||
Returns the line number.
|
||||
|
||||
===== Changes to the Line Number
|
||||
\Method IO#lineno returns the line number.
|
||||
|
||||
Reading lines from a stream usually changes its line number:
|
||||
|
||||
|
@ -460,11 +414,9 @@ that determine how lines in a stream are to be treated:
|
|||
|
||||
=== Character \IO
|
||||
|
||||
You can process an \IO stream character-by-character.
|
||||
You can process an \IO stream character-by-character using these methods:
|
||||
|
||||
===== \Method <tt>#getc</tt>
|
||||
|
||||
Reads and returns the next character from the stream:
|
||||
- IO#getc: Reads and returns the next character from the stream:
|
||||
|
||||
f = File.new('t.rus')
|
||||
f.getc # => "т"
|
||||
|
@ -473,15 +425,13 @@ Reads and returns the next character from the stream:
|
|||
f.getc # => "т"
|
||||
f.getc # => nil
|
||||
|
||||
===== \Method <tt>#readchar</tt>
|
||||
- IO#readchar (not in \StringIO):
|
||||
Like #getc, but raises an exception at end-of-stream:
|
||||
|
||||
Like #getc, but raises an exception at end-of-stream.
|
||||
f.readchar # Raises EOFError.
|
||||
|
||||
Not in \StringIO.
|
||||
|
||||
===== \Method <tt>#ungetc</tt>
|
||||
|
||||
Pushes back ("unshifts") a character or integer onto the stream:
|
||||
- IO#ungetc (not in \ARGF):
|
||||
Pushes back ("unshifts") a character or integer onto the stream:
|
||||
|
||||
path = 't.tmp'
|
||||
File.write(path, 'foo')
|
||||
|
@ -490,11 +440,7 @@ Pushes back ("unshifts") a character or integer onto the stream:
|
|||
f.read # => "тfoo"
|
||||
end
|
||||
|
||||
Not in \ARGF.
|
||||
|
||||
===== \Method <tt>#putc</tt>
|
||||
|
||||
Writes a character to the stream:
|
||||
- IO#putc (also in Kernel): Writes a character to the stream:
|
||||
|
||||
File.open('t.tmp', 'w') do |f|
|
||||
f.putc('т')
|
||||
|
@ -504,30 +450,24 @@ Writes a character to the stream:
|
|||
end
|
||||
File.read('t.tmp') # => "тест"
|
||||
|
||||
Also in Kernel.
|
||||
|
||||
===== \Method <tt>#each_char</tt>
|
||||
|
||||
Reads each remaining character in the stream,
|
||||
passing the character to the given block:
|
||||
- IO#each_char: Reads each remaining character in the stream,
|
||||
passing the character to the given block:
|
||||
|
||||
File.open('t.rus') do |f|
|
||||
f.pos = 4
|
||||
f.each_char {|c| p c }
|
||||
end
|
||||
|
||||
Output:
|
||||
Output:
|
||||
|
||||
"с"
|
||||
"т"
|
||||
|
||||
=== Byte \IO
|
||||
|
||||
You can process an \IO stream byte-by-byte.
|
||||
You can process an \IO stream byte-by-byte using these methods:
|
||||
|
||||
===== \Method <tt>#getbyte</tt>
|
||||
|
||||
Returns the next 8-bit byte as an integer in range 0..255:
|
||||
- IO#getbyte: Returns the next 8-bit byte as an integer in range 0..255:
|
||||
|
||||
File.read('t.dat')
|
||||
# => "\xFE\xFF\x99\x90\x99\x91\x99\x92\x99\x93\x99\x94"
|
||||
|
@ -541,33 +481,25 @@ Returns the next 8-bit byte as an integer in range 0..255:
|
|||
f.getbyte # => 148
|
||||
f.getbyte # => nil
|
||||
|
||||
===== \Method <tt>#readbyte</tt>
|
||||
|
||||
Like #getbyte, but raises an exception if at end-of-stream:
|
||||
- IO#readbyte (not in \StringIO):
|
||||
Like #getbyte, but raises an exception if at end-of-stream:
|
||||
|
||||
f.readbyte # Raises EOFError.
|
||||
|
||||
Not in \StringIO.
|
||||
|
||||
===== \Method <tt>#ungetbyte</tt>
|
||||
|
||||
Pushes back ("unshifts") a byte back onto the stream:
|
||||
- IO#ungetbyte (not in \ARGF):
|
||||
Pushes back ("unshifts") a byte back onto the stream:
|
||||
|
||||
f.ungetbyte(0)
|
||||
f.ungetbyte(01)
|
||||
f.read # => "\u0001\u0000"
|
||||
|
||||
Not in \ARGF.
|
||||
|
||||
===== \Method <tt>#each_byte</tt>
|
||||
|
||||
Reads each remaining byte in the stream,
|
||||
passing the byte to the given block:
|
||||
- IO#each_byte: Reads each remaining byte in the stream,
|
||||
passing the byte to the given block:
|
||||
|
||||
f.seek(-4, :END)
|
||||
f.each_byte {|b| p b }
|
||||
|
||||
Output:
|
||||
Output:
|
||||
|
||||
153
|
||||
147
|
||||
|
@ -576,12 +508,8 @@ Output:
|
|||
|
||||
=== Codepoint \IO
|
||||
|
||||
You can process an \IO stream codepoint-by-codepoint.
|
||||
|
||||
===== \Method +each_codepoint+
|
||||
|
||||
Reads each remaining codepoint in the stream,
|
||||
passing the codepoint to the given block:
|
||||
You can process an \IO stream codepoint-by-codepoint using method
|
||||
+#each_codepoint+:
|
||||
|
||||
a = []
|
||||
File.open('t.rus') do |f|
|
||||
|
|
Loading…
Reference in a new issue