rbs 3.0.0.dev.2 → 3.0.0.dev.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/comments.yml +2 -1
  3. data/.github/workflows/ruby.yml +4 -0
  4. data/Gemfile.lock +11 -11
  5. data/Rakefile +2 -2
  6. data/Steepfile +1 -1
  7. data/core/array.rbs +573 -423
  8. data/core/basic_object.rbs +11 -39
  9. data/core/binding.rbs +1 -1
  10. data/core/builtin.rbs +8 -0
  11. data/core/class.rbs +37 -0
  12. data/core/comparable.rbs +7 -18
  13. data/core/complex.rbs +2 -2
  14. data/core/data.rbs +419 -0
  15. data/core/dir.rbs +52 -104
  16. data/core/encoding.rbs +22 -181
  17. data/core/enumerable.rbs +212 -175
  18. data/core/enumerator/product.rbs +96 -0
  19. data/core/enumerator.rbs +57 -8
  20. data/core/errors.rbs +8 -2
  21. data/core/exception.rbs +41 -0
  22. data/core/fiber.rbs +95 -12
  23. data/core/file.rbs +840 -275
  24. data/core/file_test.rbs +34 -19
  25. data/core/float.rbs +40 -96
  26. data/core/gc.rbs +15 -3
  27. data/core/hash.rbs +113 -175
  28. data/core/integer.rbs +85 -145
  29. data/core/io/buffer.rbs +187 -60
  30. data/core/io/wait.rbs +28 -16
  31. data/core/io.rbs +1859 -1389
  32. data/core/kernel.rbs +525 -961
  33. data/core/match_data.rbs +306 -142
  34. data/core/math.rbs +506 -234
  35. data/core/method.rbs +0 -24
  36. data/core/module.rbs +110 -17
  37. data/core/nil_class.rbs +2 -0
  38. data/core/numeric.rbs +76 -144
  39. data/core/object.rbs +88 -212
  40. data/core/proc.rbs +17 -5
  41. data/core/process.rbs +22 -5
  42. data/core/ractor.rbs +1 -1
  43. data/core/random.rbs +20 -3
  44. data/core/range.rbs +91 -89
  45. data/core/rational.rbs +2 -3
  46. data/core/rbs/unnamed/argf.rbs +177 -120
  47. data/core/rbs/unnamed/env_class.rbs +89 -163
  48. data/core/rbs/unnamed/random.rbs +36 -12
  49. data/core/refinement.rbs +8 -0
  50. data/core/regexp.rbs +462 -272
  51. data/core/ruby_vm.rbs +210 -0
  52. data/{stdlib/set/0 → core}/set.rbs +43 -47
  53. data/core/string.rbs +1403 -1332
  54. data/core/string_io.rbs +191 -107
  55. data/core/struct.rbs +67 -63
  56. data/core/symbol.rbs +187 -201
  57. data/core/thread.rbs +40 -35
  58. data/core/time.rbs +902 -826
  59. data/core/trace_point.rbs +55 -6
  60. data/core/unbound_method.rbs +48 -24
  61. data/docs/collection.md +4 -0
  62. data/docs/syntax.md +55 -0
  63. data/ext/rbs_extension/parser.c +5 -6
  64. data/lib/rbs/cli.rb +6 -1
  65. data/lib/rbs/collection/cleaner.rb +8 -1
  66. data/lib/rbs/collection/config/lockfile.rb +3 -1
  67. data/lib/rbs/collection/config/lockfile_generator.rb +16 -14
  68. data/lib/rbs/collection/config.rb +1 -1
  69. data/lib/rbs/collection/sources/git.rb +9 -2
  70. data/lib/rbs/collection/sources/local.rb +79 -0
  71. data/lib/rbs/collection/sources.rb +8 -1
  72. data/lib/rbs/environment.rb +6 -5
  73. data/lib/rbs/environment_loader.rb +3 -2
  74. data/lib/rbs/errors.rb +18 -0
  75. data/lib/rbs/locator.rb +26 -7
  76. data/lib/rbs/sorter.rb +2 -2
  77. data/lib/rbs/version.rb +1 -1
  78. data/sig/collection/sources.rbs +32 -3
  79. data/sig/environment.rbs +2 -3
  80. data/sig/locator.rbs +14 -2
  81. data/sig/shims/{abstract_syntax_tree.rbs → _abstract_syntax_tree.rbs} +0 -0
  82. data/stdlib/bigdecimal/0/big_decimal.rbs +16 -13
  83. data/stdlib/cgi/0/core.rbs +16 -0
  84. data/stdlib/coverage/0/coverage.rbs +50 -8
  85. data/stdlib/csv/0/csv.rbs +1 -1
  86. data/stdlib/date/0/date.rbs +856 -726
  87. data/stdlib/date/0/date_time.rbs +83 -210
  88. data/stdlib/erb/0/erb.rbs +13 -36
  89. data/stdlib/etc/0/etc.rbs +127 -20
  90. data/stdlib/fileutils/0/fileutils.rbs +1290 -381
  91. data/stdlib/logger/0/logger.rbs +466 -316
  92. data/stdlib/net-http/0/net-http.rbs +2211 -534
  93. data/stdlib/nkf/0/nkf.rbs +5 -5
  94. data/stdlib/objspace/0/objspace.rbs +31 -14
  95. data/stdlib/openssl/0/openssl.rbs +11 -7
  96. data/stdlib/optparse/0/optparse.rbs +20 -17
  97. data/stdlib/pathname/0/pathname.rbs +21 -4
  98. data/stdlib/pstore/0/pstore.rbs +378 -154
  99. data/stdlib/pty/0/pty.rbs +24 -8
  100. data/stdlib/ripper/0/ripper.rbs +1650 -0
  101. data/stdlib/socket/0/addrinfo.rbs +9 -15
  102. data/stdlib/socket/0/socket.rbs +36 -3
  103. data/stdlib/strscan/0/string_scanner.rbs +7 -5
  104. data/stdlib/tempfile/0/tempfile.rbs +104 -44
  105. data/stdlib/time/0/time.rbs +2 -2
  106. data/stdlib/uri/0/file.rbs +5 -0
  107. data/stdlib/uri/0/generic.rbs +2 -2
  108. data/stdlib/yaml/0/yaml.rbs +2 -2
  109. data/stdlib/zlib/0/zlib.rbs +1 -1
  110. metadata +8 -6
  111. data/core/deprecated.rbs +0 -9
  112. data/sig/shims/ripper.rbs +0 -8
data/core/io.rbs CHANGED
@@ -1,230 +1,94 @@
1
1
  # <!-- rdoc-file=io.c -->
2
- # The IO class is the basis for all input and output in Ruby. An I/O stream may
3
- # be *duplexed* (that is, bidirectional), and so may use more than one native
4
- # operating system stream.
2
+ # An instance of class IO (commonly called a *stream*) represents an
3
+ # input/output stream in the underlying operating system. Class IO is the basis
4
+ # for input and output in Ruby.
5
5
  #
6
- # Many of the examples in this section use the File class, the only standard
7
- # subclass of IO. The two classes are closely associated. Like the File class,
8
- # the Socket library subclasses from IO (such as TCPSocket or UDPSocket).
9
- #
10
- # The Kernel#open method can create an IO (or File) object for these types of
11
- # arguments:
12
- #
13
- # * A plain string represents a filename suitable for the underlying operating
14
- # system.
15
- #
16
- # * A string starting with `"|"` indicates a subprocess. The remainder of the
17
- # string following the `"|"` is invoked as a process with appropriate
18
- # input/output channels connected to it.
19
- #
20
- # * A string equal to `"|-"` will create another Ruby instance as a
21
- # subprocess.
22
- #
23
- #
24
- # The IO may be opened with different file modes (read-only, write-only) and
25
- # encodings for proper conversion. See IO.new for these options. See
26
- # Kernel#open for details of the various command formats described above.
27
- #
28
- # IO.popen, the Open3 library, or Process#spawn may also be used to communicate
29
- # with subprocesses through an IO.
30
- #
31
- # Ruby will convert pathnames between different operating system conventions if
32
- # possible. For instance, on a Windows system the filename
33
- # `"/gumby/ruby/test.rb"` will be opened as `"\gumby\ruby\test.rb"`. When
34
- # specifying a Windows-style filename in a Ruby string, remember to escape the
35
- # backslashes:
36
- #
37
- # "C:\\gumby\\ruby\\test.rb"
38
- #
39
- # Our examples here will use the Unix-style forward slashes; File::ALT_SEPARATOR
40
- # can be used to get the platform-specific separator character.
6
+ # Class File is the only class in the Ruby core that is a subclass of IO. Some
7
+ # classes in the Ruby standard library are also subclasses of IO; these include
8
+ # TCPSocket and UDPSocket.
41
9
  #
42
10
  # The global constant ARGF (also accessible as `$<`) provides an IO-like stream
43
- # which allows access to all files mentioned on the command line (or STDIN if no
44
- # files are mentioned). ARGF#path and its alias ARGF#filename are provided to
45
- # access the name of the file currently being read.
46
- #
47
- # ## io/console
48
- #
49
- # The io/console extension provides methods for interacting with the console.
50
- # The console can be accessed from IO.console or the standard input/output/error
51
- # IO objects.
52
- #
53
- # Requiring io/console adds the following methods:
54
- #
55
- # * IO::console
56
- # * IO#raw
57
- # * IO#raw!
58
- # * IO#cooked
59
- # * IO#cooked!
60
- # * IO#getch
61
- # * IO#echo=
62
- # * IO#echo?
63
- # * IO#noecho
64
- # * IO#winsize
65
- # * IO#winsize=
66
- # * IO#iflush
67
- # * IO#ioflush
68
- # * IO#oflush
69
- #
70
- #
71
- # Example:
72
- #
73
- # require 'io/console'
74
- # rows, columns = $stdout.winsize
75
- # puts "Your screen is #{columns} wide and #{rows} tall"
76
- #
77
- # ## Example Files
78
- #
79
- # Many examples here use these filenames and their corresponding files:
80
- #
81
- # * `t.txt`: A text-only file that is assumed to exist via:
82
- #
83
- # text = <<~EOT
84
- # This is line one.
85
- # This is the second line.
86
- # This is the third line.
87
- # EOT
88
- # File.write('t.txt', text)
89
- #
90
- # * `t.dat`: A data file that is assumed to exist via:
91
- #
92
- # data = "\u9990\u9991\u9992\u9993\u9994"
93
- # f = File.open('t.dat', 'wb:UTF-16')
94
- # f.write(data)
95
- # f.close
11
+ # that allows access to all file paths found in ARGV (or found in STDIN if ARGV
12
+ # is empty). ARGF is not itself a subclass of IO.
96
13
  #
97
- # * `t.rus`: A Russian-language text file that is assumed to exist via:
14
+ # Class StringIO provides an IO-like stream that handles a String. StringIO is
15
+ # not itself a subclass of IO.
98
16
  #
99
- # File.write('t.rus', "\u{442 435 441 442}")
17
+ # Important objects based on IO include:
100
18
  #
101
- # * `t.tmp`: A file that is assumed *not* to exist.
19
+ # * $stdin.
20
+ # * $stdout.
21
+ # * $stderr.
22
+ # * Instances of class File.
102
23
  #
103
24
  #
104
- # ## Modes
25
+ # An instance of IO may be created using:
105
26
  #
106
- # A number of IO method calls must or may specify a *mode* for the stream; the
107
- # mode determines how stream is to be accessible, including:
27
+ # * IO.new: returns a new IO object for the given integer file descriptor.
28
+ # * IO.open: passes a new IO object to the given block.
29
+ # * IO.popen: returns a new IO object that is connected to the $stdin and
30
+ # $stdout of a newly-launched subprocess.
31
+ # * Kernel#open: Returns a new IO object connected to a given source: stream,
32
+ # file, or subprocess.
108
33
  #
109
- # * Whether the stream is to be read-only, write-only, or read-write.
110
- # * Whether the stream is positioned at its beginning or its end.
111
- # * Whether the stream treats data as text-only or binary.
112
- # * The external and internal encodings.
113
34
  #
35
+ # Like a File stream, an IO stream has:
114
36
  #
115
- # ### Mode Specified as an Integer
37
+ # * A read/write mode, which may be read-only, write-only, or read/write; see
38
+ # [Read/Write Mode](rdoc-ref:File@Read-2FWrite+Mode).
39
+ # * A data mode, which may be text-only or binary; see [Data
40
+ # Mode](rdoc-ref:File@Data+Mode).
41
+ # * Internal and external encodings; see [Encodings](rdoc-ref:File@Encodings).
116
42
  #
117
- # When `mode` is an integer it must be one or more (combined by bitwise OR (`|`)
118
- # of the modes defined in File::Constants:
119
43
  #
120
- # * `File::RDONLY`: Open for reading only.
121
- # * `File::WRONLY`: Open for writing only.
122
- # * `File::RDWR`: Open for reading and writing.
123
- # * `File::APPEND`: Open for appending only.
124
- # * `File::CREAT`: Create file if it does not exist.
125
- # * `File::EXCL`: Raise an exception if `File::CREAT` is given and the file
126
- # exists.
44
+ # And like other IO streams, it has:
127
45
  #
46
+ # * A position, which determines where in the stream the next read or write is
47
+ # to occur; see [Position](rdoc-ref:IO@Position).
48
+ # * A line number, which is a special, line-oriented, "position" (different
49
+ # from the position mentioned above); see [Line
50
+ # Number](rdoc-ref:IO@Line+Number).
128
51
  #
129
- # Examples:
130
52
  #
131
- # File.new('t.txt', File::RDONLY)
132
- # File.new('t.tmp', File::RDWR | File::CREAT | File::EXCL)
53
+ # ## Extension `io/console`
133
54
  #
134
- # Note: Method IO#set_encoding does not allow the mode to be specified as an
135
- # integer.
55
+ # Extension `io/console` provides numerous methods for interacting with the
56
+ # console; requiring it adds numerous methods to class IO.
136
57
  #
137
- # ### Mode Specified As a String
138
- #
139
- # When `mode` is a string it must begin with one of the following:
140
- #
141
- # * `'r'`: Read-only stream, positioned at the beginning; the stream cannot be
142
- # changed to writable.
143
- # * `'w'`: Write-only stream, positioned at the beginning; the stream cannot
144
- # be changed to readable.
145
- # * `'a'`: Write-only stream, positioned at the end; every write appends to
146
- # the end; the stream cannot be changed to readable.
147
- # * `'r+'`: Read-write stream, positioned at the beginning.
148
- # * `'w+'`: Read-write stream, positioned at the end.
149
- # * `'a+'`: Read-write stream, positioned at the end.
150
- #
151
- #
152
- # For a writable file stream (that is, any except read-only), the file is
153
- # truncated to zero if it exists, and is created if it does not exist.
154
- #
155
- # Examples:
156
- #
157
- # File.open('t.txt', 'r')
158
- # File.open('t.tmp', 'w')
159
- #
160
- # Either of the following may be suffixed to any of the above:
161
- #
162
- # * `'t'`: Text data; sets the default external encoding to `Encoding::UTF_8`;
163
- # on Windows, enables conversion between EOL and CRLF.
164
- # * `'b'`: Binary data; sets the default external encoding to
165
- # `Encoding::ASCII_8BIT`; on Windows, suppresses conversion between EOL and
166
- # CRLF.
167
- #
168
- #
169
- # If neither is given, the stream defaults to text data.
170
- #
171
- # Examples:
172
- #
173
- # File.open('t.txt', 'rt')
174
- # File.open('t.dat', 'rb')
175
- #
176
- # The following may be suffixed to any writable mode above:
177
- #
178
- # * `'x'`: Creates the file if it does not exist; raises an exception if the
179
- # file exists.
180
- #
181
- #
182
- # Example:
183
- #
184
- # File.open('t.tmp', 'wx')
185
- #
186
- # Finally, the mode string may specify encodings -- either external encoding
187
- # only or both external and internal encodings -- by appending one or both
188
- # encoding names, separated by colons:
58
+ # ## Example Files
189
59
  #
190
- # f = File.new('t.dat', 'rb')
191
- # f.external_encoding # => #<Encoding:ASCII-8BIT>
192
- # f.internal_encoding # => nil
193
- # f = File.new('t.dat', 'rb:UTF-16')
194
- # f.external_encoding # => #<Encoding:UTF-16 (dummy)>
195
- # f.internal_encoding # => nil
196
- # f = File.new('t.dat', 'rb:UTF-16:UTF-16')
197
- # f.external_encoding # => #<Encoding:UTF-16 (dummy)>
198
- # f.internal_encoding # => #<Encoding:UTF-16>
60
+ # Many examples here use these variables:
199
61
  #
200
- # The numerous encoding names are available in array Encoding.name_list:
62
+ # # English text with newlines.
63
+ # text = <<~EOT
64
+ # First line
65
+ # Second line
201
66
  #
202
- # Encoding.name_list.size # => 175
203
- # Encoding.name_list.take(3) # => ["ASCII-8BIT", "UTF-8", "US-ASCII"]
67
+ # Fourth line
68
+ # Fifth line
69
+ # EOT
204
70
  #
205
- # ## Encodings
71
+ # # Russian text.
72
+ # russian = "\u{442 435 441 442}" # => "тест"
206
73
  #
207
- # When the external encoding is set, strings read are tagged by that encoding
208
- # when reading, and strings written are converted to that encoding when writing.
74
+ # # Binary data.
75
+ # data = "\u9990\u9991\u9992\u9993\u9994"
209
76
  #
210
- # When both external and internal encodings are set, strings read are converted
211
- # from external to internal encoding, and strings written are converted from
212
- # internal to external encoding. For further details about transcoding input and
213
- # output, see Encoding.
77
+ # # Text file.
78
+ # File.write('t.txt', text)
214
79
  #
215
- # If the external encoding is `'BOM|UTF-8'`, `'BOM|UTF-16LE'` or
216
- # `'BOM|UTF16-BE'`, Ruby checks for a Unicode BOM in the input document to help
217
- # determine the encoding. For UTF-16 encodings the file open mode must be
218
- # binary. If the BOM is found, it is stripped and the external encoding from the
219
- # BOM is used.
80
+ # # File with Russian text.
81
+ # File.write('t.rus', russian)
220
82
  #
221
- # Note that the BOM-style encoding option is case insensitive, so 'bom|utf-8' is
222
- # also valid.)
83
+ # # File with binary data.
84
+ # f = File.new('t.dat', 'wb:UTF-16')
85
+ # f.write(data)
86
+ # f.close
223
87
  #
224
88
  # ## Open Options
225
89
  #
226
- # A number of IO methods accept an optional parameter `opts`, which determines
227
- # how a new stream is to be opened:
90
+ # A number of IO methods accept optional keyword arguments that determine how a
91
+ # new stream is to be opened:
228
92
  #
229
93
  # * `:mode`: Stream mode.
230
94
  # * `:flags`: Integer file open flags; If `mode` is also given, the two are
@@ -241,430 +105,520 @@
241
105
  # otherwise.
242
106
  # * `:autoclose`: If a truthy value, specifies that the `fd` will close when
243
107
  # the stream closes; otherwise it remains open.
108
+ # * `:path:` If a string value is provided, it is used in #inspect and is
109
+ # available as #path method.
244
110
  #
245
111
  #
246
112
  # Also available are the options offered in String#encode, which may control
247
113
  # conversion between external internal encoding.
248
114
  #
249
- # ## Getline Options
115
+ # ## Basic IO
250
116
  #
251
- # A number of IO methods accept optional keyword arguments that determine how a
252
- # stream is to be treated:
117
+ # You can perform basic stream IO with these methods, which typically operate on
118
+ # multi-byte strings:
253
119
  #
254
- # * `:chomp`: If `true`, line separators are omitted; default is `false`.
120
+ # * IO#read: Reads and returns some or all of the remaining bytes from the
121
+ # stream.
122
+ # * IO#write: Writes zero or more strings to the stream; each given object
123
+ # that is not already a string is converted via `to_s`.
255
124
  #
256
125
  #
257
- # ## Position
126
+ # ### Position
258
127
  #
259
- # An IO stream has a *position*, which is the non-negative integer offset (in
260
- # bytes) in the stream where the next read or write will occur.
128
+ # An IO stream has a nonnegative integer *position*, which is the byte offset at
129
+ # which the next read or write is to occur. A new stream has position zero (and
130
+ # line number zero); method `rewind` resets the position (and line number) to
131
+ # zero.
261
132
  #
262
- # Note that a text stream may have multi-byte characters, so a text stream whose
263
- # position is `n` (*bytes*) may not have `n` *characters* preceding the current
264
- # position -- there may be fewer.
133
+ # The relevant methods:
265
134
  #
266
- # A new stream is initially positioned:
135
+ # * IO#tell (aliased as `#pos`): Returns the current position (in bytes) in
136
+ # the stream.
137
+ # * IO#pos=: Sets the position of the stream to a given integer `new_position`
138
+ # (in bytes).
139
+ # * IO#seek: Sets the position of the stream to a given integer `offset` (in
140
+ # bytes), relative to a given position `whence` (indicating the beginning,
141
+ # end, or current position).
142
+ # * IO#rewind: Positions the stream at the beginning (also resetting the line
143
+ # number).
267
144
  #
268
- # * At the beginning (position `0`) if its mode is `'r'`, `'w'`, or `'r+'`.
269
- # * At the end (position `self.size`) if its mode is `'a'`, `'w+'`, or `'a+'`.
270
145
  #
146
+ # ### Open and Closed Streams
271
147
  #
272
- # Methods to query the position:
148
+ # A new IO stream may be open for reading, open for writing, or both.
273
149
  #
274
- # * IO#tell and its alias IO#pos return the position for an open stream.
275
- # * IO#eof? and its alias IO#eof return whether the position is at the end of
276
- # a readable stream.
150
+ # A stream is automatically closed when claimed by the garbage collector.
277
151
  #
152
+ # Attempted reading or writing on a closed stream raises an exception.
278
153
  #
279
- # Reading from a stream usually changes its position:
154
+ # The relevant methods:
280
155
  #
281
- # f = File.open('t.txt')
282
- # f.tell # => 0
283
- # f.readline # => "This is line one.\n"
284
- # f.tell # => 19
285
- # f.readline # => "This is the second line.\n"
286
- # f.tell # => 45
287
- # f.eof? # => false
288
- # f.readline # => "Here's the third line.\n"
289
- # f.eof? # => true
156
+ # * IO#close: Closes the stream for both reading and writing.
157
+ # * IO#close_read: Closes the stream for reading.
158
+ # * IO#close_write: Closes the stream for writing.
159
+ # * IO#closed?: Returns whether the stream is closed.
290
160
  #
291
- # Writing to a stream usually changes its position:
292
161
  #
293
- # f = File.open('t.tmp', 'w')
294
- # f.tell # => 0
295
- # f.write('foo') # => 3
296
- # f.tell # => 3
297
- # f.write('bar') # => 3
298
- # f.tell # => 6
162
+ # ### End-of-Stream
299
163
  #
300
- # Iterating over a stream usually changes its position:
164
+ # You can query whether a stream is positioned at its end:
301
165
  #
302
- # f = File.open('t.txt')
303
- # f.each do |line|
304
- # p "position=#{f.pos} eof?=#{f.eof?} line=#{line}"
305
- # end
166
+ # * IO#eof? (also aliased as `#eof`): Returns whether the stream is at
167
+ # end-of-stream.
306
168
  #
307
- # Output:
308
169
  #
309
- # "position=19 eof?=false line=This is line one.\n"
310
- # "position=45 eof?=false line=This is the second line.\n"
311
- # "position=70 eof?=true line=This is the third line.\n"
170
+ # You can reposition to end-of-stream by using method IO#seek:
312
171
  #
313
- # The position may also be changed by certain other methods:
172
+ # f = File.new('t.txt')
173
+ # f.eof? # => false
174
+ # f.seek(0, :END)
175
+ # f.eof? # => true
176
+ # f.close
314
177
  #
315
- # * IO#pos= and IO#seek change the position to a specified offset.
316
- # * IO#rewind changes the position to the beginning.
178
+ # Or by reading all stream content (which is slower than using IO#seek):
317
179
  #
180
+ # f.rewind
181
+ # f.eof? # => false
182
+ # f.read # => "First line\nSecond line\n\nFourth line\nFifth line\n"
183
+ # f.eof? # => true
318
184
  #
319
- # ## Line Number
185
+ # ## Line IO
320
186
  #
321
- # A readable IO stream has a *line* *number*, which is the non-negative integer
322
- # line number in the stream where the next read will occur.
187
+ # You can read an IO stream line-by-line using these methods:
323
188
  #
324
- # A new stream is initially has line number `0`.
189
+ # * IO#each_line: Reads each remaining line, passing it to the given block.
190
+ # * IO#gets: Returns the next line.
191
+ # * IO#readline: Like #gets, but raises an exception at end-of-stream.
192
+ # * IO#readlines: Returns all remaining lines in an array.
325
193
  #
326
- # Method IO#lineno returns the line number.
327
194
  #
328
- # Reading lines from a stream usually changes its line number:
195
+ # Each of these reader methods accepts:
329
196
  #
330
- # f = File.open('t.txt', 'r')
331
- # f.lineno # => 0
332
- # f.readline # => "This is line one.\n"
333
- # f.lineno # => 1
334
- # f.readline # => "This is the second line.\n"
335
- # f.lineno # => 2
336
- # f.readline # => "Here's the third line.\n"
337
- # f.lineno # => 3
338
- # f.eof? # => true
197
+ # * An optional line separator, `sep`; see [Line
198
+ # Separator](rdoc-ref:IO@Line+Separator).
199
+ # * An optional line-size limit, `limit`; see [Line
200
+ # Limit](rdoc-ref:IO@Line+Limit).
339
201
  #
340
- # Iterating over lines in a stream usually changes its line number:
341
202
  #
342
- # f = File.open('t.txt')
343
- # f.each_line do |line|
344
- # p "position=#{f.pos} eof?=#{f.eof?} line=#{line}"
345
- # end
203
+ # For each of these reader methods, reading may begin mid-line, depending on the
204
+ # stream's position; see [Position](rdoc-ref:IO@Position):
346
205
  #
347
- # Output:
206
+ # f = File.new('t.txt')
207
+ # f.pos = 27
208
+ # f.each_line {|line| p line }
209
+ # f.close
348
210
  #
349
- # "position=19 eof?=false line=This is line one.\n"
350
- # "position=45 eof?=false line=This is the second line.\n"
351
- # "position=70 eof?=true line=This is the third line.\n"
352
- #
353
- # ## What's Here
211
+ # Output:
354
212
  #
355
- # First, what's elsewhere. Class IO:
213
+ # "rth line\n"
214
+ # "Fifth line\n"
356
215
  #
357
- # * Inherits from [class
358
- # Object](Object.html#class-Object-label-What-27s+Here).
359
- # * Includes [module
360
- # Enumerable](Enumerable.html#module-Enumerable-label-What-27s+Here), which
361
- # provides dozens of additional methods.
216
+ # You can write to an IO stream line-by-line using this method:
362
217
  #
218
+ # * IO#puts: Writes objects to the stream.
363
219
  #
364
- # Here, class IO provides methods that are useful for:
365
220
  #
366
- # * [Creating](#class-IO-label-Creating)
367
- # * [Reading](#class-IO-label-Reading)
368
- # * [Writing](#class-IO-label-Writing)
369
- # * [Positioning](#class-IO-label-Positioning)
370
- # * [Iterating](#class-IO-label-Iterating)
371
- # * [Settings](#class-IO-label-Settings)
372
- # * [Querying](#class-IO-label-Querying)
373
- # * [Buffering](#class-IO-label-Buffering)
374
- # * [Low-Level Access](#class-IO-label-Low-Level+Access)
375
- # * [Other](#class-IO-label-Other)
221
+ # ### Line Separator
376
222
  #
223
+ # Each of these methods uses a *line separator*, which is the string that
224
+ # delimits lines:
377
225
  #
378
- # ### Creating
226
+ # * IO.foreach.
227
+ # * IO.readlines.
228
+ # * IO#each_line.
229
+ # * IO#gets.
230
+ # * IO#readline.
231
+ # * IO#readlines.
379
232
  #
380
- # ::new (aliased as ::for_fd)
381
- # : Creates and returns a new IO object for the given integer file
382
- # descriptor.
383
233
  #
384
- # ::open
385
- # : Creates a new IO object.
234
+ # The default line separator is the given by the global variable `$/`, whose
235
+ # value is by default `"\n"`. The line to be read next is all data from the
236
+ # current position to the next line separator:
386
237
  #
387
- # ::pipe
388
- # : Creates a connected pair of reader and writer IO objects.
238
+ # f = File.new('t.txt')
239
+ # f.gets # => "First line\n"
240
+ # f.gets # => "Second line\n"
241
+ # f.gets # => "\n"
242
+ # f.gets # => "Fourth line\n"
243
+ # f.gets # => "Fifth line\n"
244
+ # f.close
389
245
  #
390
- # ::popen
391
- # : Creates an IO object to interact with a subprocess.
246
+ # You can specify a different line separator:
392
247
  #
393
- # ::select
394
- # : Selects which given IO instances are ready for reading,
248
+ # f = File.new('t.txt')
249
+ # f.gets('l') # => "First l"
250
+ # f.gets('li') # => "ine\nSecond li"
251
+ # f.gets('lin') # => "ne\n\nFourth lin"
252
+ # f.gets # => "e\n"
253
+ # f.close
395
254
  #
396
- # writing, or have pending exceptions.
255
+ # There are two special line separators:
397
256
  #
257
+ # * `nil`: The entire stream is read into a single string:
398
258
  #
399
- # ### Reading
259
+ # f = File.new('t.txt')
260
+ # f.gets(nil) # => "First line\nSecond line\n\nFourth line\nFifth line\n"
261
+ # f.close
400
262
  #
401
- # ::binread
402
- # : Returns a binary string with all or a subset of bytes from the given
403
- # file.
263
+ # * `''` (the empty string): The next "paragraph" is read (paragraphs being
264
+ # separated by two consecutive line separators):
404
265
  #
405
- # ::read
406
- # : Returns a string with all or a subset of bytes from the given file.
266
+ # f = File.new('t.txt')
267
+ # f.gets('') # => "First line\nSecond line\n\n"
268
+ # f.gets('') # => "Fourth line\nFifth line\n"
269
+ # f.close
407
270
  #
408
- # ::readlines
409
- # : Returns an array of strings, which are the lines from the given file.
410
271
  #
411
- # #getbyte
412
- # : Returns the next 8-bit byte read from `self` as an integer.
272
+ # ### Line Limit
413
273
  #
414
- # #getc
415
- # : Returns the next character read from `self` as a string.
274
+ # Each of these methods uses a *line limit*, which specifies that the number of
275
+ # bytes returned may not be (much) longer than the given `limit`;
416
276
  #
417
- # #gets
418
- # : Returns the line read from `self`.
277
+ # * IO.foreach.
278
+ # * IO.readlines.
279
+ # * IO#each_line.
280
+ # * IO#gets.
281
+ # * IO#readline.
282
+ # * IO#readlines.
419
283
  #
420
- # #pread
421
- # : Returns all or the next *n* bytes read from `self`, not updating the
422
- # receiver's offset.
423
284
  #
424
- # #read
425
- # : Returns all remaining or the next *n* bytes read from `self` for a
426
- # given *n*.
285
+ # A multi-byte character will not be split, and so a line may be slightly longer
286
+ # than the given limit.
427
287
  #
428
- # #read_nonblock
429
- # : the next *n* bytes read from `self` for a given *n*, in non-block
430
- # mode.
288
+ # If `limit` is not given, the line is determined only by `sep`.
431
289
  #
432
- # #readbyte
433
- # : Returns the next byte read from `self`; same as #getbyte, but raises
434
- # an exception on end-of-file.
290
+ # # Text with 1-byte characters.
291
+ # File.open('t.txt') {|f| f.gets(1) } # => "F"
292
+ # File.open('t.txt') {|f| f.gets(2) } # => "Fi"
293
+ # File.open('t.txt') {|f| f.gets(3) } # => "Fir"
294
+ # File.open('t.txt') {|f| f.gets(4) } # => "Firs"
295
+ # # No more than one line.
296
+ # File.open('t.txt') {|f| f.gets(10) } # => "First line"
297
+ # File.open('t.txt') {|f| f.gets(11) } # => "First line\n"
298
+ # File.open('t.txt') {|f| f.gets(12) } # => "First line\n"
435
299
  #
436
- # #readchar
437
- # : Returns the next character read from `self`; same as #getc, but raises
438
- # an exception on end-of-file.
300
+ # # Text with 2-byte characters, which will not be split.
301
+ # File.open('t.rus') {|f| f.gets(1).size } # => 1
302
+ # File.open('t.rus') {|f| f.gets(2).size } # => 1
303
+ # File.open('t.rus') {|f| f.gets(3).size } # => 2
304
+ # File.open('t.rus') {|f| f.gets(4).size } # => 2
439
305
  #
440
- # #readline
441
- # : Returns the next line read from `self`; same as #getline, but raises
442
- # an exception of end-of-file.
306
+ # ### Line Separator and Line Limit
443
307
  #
444
- # #readlines
445
- # : Returns an array of all lines read read from `self`.
308
+ # With arguments `sep` and `limit` given, combines the two behaviors:
446
309
  #
447
- # #readpartial
448
- # : Returns up to the given number of bytes from `self`.
310
+ # * Returns the next line as determined by line separator `sep`.
311
+ # * But returns no more bytes than are allowed by the limit.
449
312
  #
450
313
  #
314
+ # Example:
451
315
  #
452
- # ### Writing
316
+ # File.open('t.txt') {|f| f.gets('li', 20) } # => "First li"
317
+ # File.open('t.txt') {|f| f.gets('li', 2) } # => "Fi"
453
318
  #
454
- # ::binwrite
455
- # : Writes the given string to the file at the given filepath, in binary
456
- # mode.
319
+ # ### Line Number
457
320
  #
458
- # ::write
459
- # : Writes the given string to `self`.
321
+ # A readable IO stream has a non-negative integer *line number*.
460
322
  #
461
- # [:<<](#method-i-3C-3C)
462
- # : Appends the given string to `self`.
323
+ # The relevant methods:
463
324
  #
464
- # #print
465
- # : Prints last read line or given objects to `self`.
325
+ # * IO#lineno: Returns the line number.
326
+ # * IO#lineno=: Resets and returns the line number.
466
327
  #
467
- # #printf
468
- # : Writes to `self` based on the given format string and objects.
469
328
  #
470
- # #putc
471
- # : Writes a character to `self`.
329
+ # Unless modified by a call to method IO#lineno=, the line number is the number
330
+ # of lines read by certain line-oriented methods, according to the given line
331
+ # separator `sep`:
472
332
  #
473
- # #puts
474
- # : Writes lines to `self`, making sure line ends with a newline.
333
+ # * IO.foreach: Increments the line number on each call to the block.
334
+ # * IO#each_line: Increments the line number on each call to the block.
335
+ # * IO#gets: Increments the line number.
336
+ # * IO#readline: Increments the line number.
337
+ # * IO#readlines: Increments the line number for each line read.
475
338
  #
476
- # #pwrite
477
- # : Writes the given string at the given offset, not updating the
478
- # receiver's offset.
479
339
  #
480
- # #write
481
- # : Writes one or more given strings to `self`.
340
+ # A new stream is initially has line number zero (and position zero); method
341
+ # `rewind` resets the line number (and position) to zero:
482
342
  #
483
- # #write_nonblock
484
- # : Writes one or more given strings to `self` in non-blocking mode.
343
+ # f = File.new('t.txt')
344
+ # f.lineno # => 0
345
+ # f.gets # => "First line\n"
346
+ # f.lineno # => 1
347
+ # f.rewind
348
+ # f.lineno # => 0
349
+ # f.close
485
350
  #
351
+ # Reading lines from a stream usually changes its line number:
486
352
  #
353
+ # f = File.new('t.txt', 'r')
354
+ # f.lineno # => 0
355
+ # f.readline # => "This is line one.\n"
356
+ # f.lineno # => 1
357
+ # f.readline # => "This is the second line.\n"
358
+ # f.lineno # => 2
359
+ # f.readline # => "Here's the third line.\n"
360
+ # f.lineno # => 3
361
+ # f.eof? # => true
362
+ # f.close
487
363
  #
488
- # ### Positioning
364
+ # Iterating over lines in a stream usually changes its line number:
489
365
  #
490
- # #lineno
491
- # : Returns the current line number in `self`.
366
+ # File.open('t.txt') do |f|
367
+ # f.each_line do |line|
368
+ # p "position=#{f.pos} eof?=#{f.eof?} lineno=#{f.lineno}"
369
+ # end
370
+ # end
492
371
  #
493
- # #lineno=
494
- # : Sets the line number is `self`.
372
+ # Output:
495
373
  #
496
- # #pos (aliased as #tell)
497
- # : Returns the current byte offset in `self`.
374
+ # "position=11 eof?=false lineno=1"
375
+ # "position=23 eof?=false lineno=2"
376
+ # "position=24 eof?=false lineno=3"
377
+ # "position=36 eof?=false lineno=4"
378
+ # "position=47 eof?=true lineno=5"
498
379
  #
499
- # #pos=
500
- # : Sets the byte offset in `self`.
380
+ # Unlike the stream's [position](rdoc-ref:IO@Position), the line number does not
381
+ # affect where the next read or write will occur:
501
382
  #
502
- # #reopen
503
- # : Reassociates `self` with a new or existing IO stream.
383
+ # f = File.new('t.txt')
384
+ # f.lineno = 1000
385
+ # f.lineno # => 1000
386
+ # f.gets # => "First line\n"
387
+ # f.lineno # => 1001
388
+ # f.close
504
389
  #
505
- # #rewind
506
- # : Positions `self` to the beginning of input.
390
+ # Associated with the line number is the global variable `$.`:
507
391
  #
508
- # #seek
509
- # : Sets the offset for `self` relative to given position.
392
+ # * When a stream is opened, `$.` is not set; its value is left over from
393
+ # previous activity in the process:
510
394
  #
395
+ # $. = 41
396
+ # f = File.new('t.txt')
397
+ # $. = 41
398
+ # # => 41
399
+ # f.close
511
400
  #
401
+ # * When a stream is read, `#.` is set to the line number for that stream:
402
+ #
403
+ # f0 = File.new('t.txt')
404
+ # f1 = File.new('t.dat')
405
+ # f0.readlines # => ["First line\n", "Second line\n", "\n", "Fourth line\n", "Fifth line\n"]
406
+ # $. # => 5
407
+ # f1.readlines # => ["\xFE\xFF\x99\x90\x99\x91\x99\x92\x99\x93\x99\x94"]
408
+ # $. # => 1
409
+ # f0.close
410
+ # f1.close
411
+ #
412
+ # * Methods IO#rewind and IO#seek do not affect `$.`:
413
+ #
414
+ # f = File.new('t.txt')
415
+ # f.readlines # => ["First line\n", "Second line\n", "\n", "Fourth line\n", "Fifth line\n"]
416
+ # $. # => 5
417
+ # f.rewind
418
+ # f.seek(0, :SET)
419
+ # $. # => 5
420
+ # f.close
512
421
  #
513
- # ### Iterating
514
422
  #
515
- # ::foreach
516
- # : Yields each line of given file to the block.
423
+ # ## Character IO
517
424
  #
518
- # #each (aliased as #each_line)
519
- # : Calls the given block with each successive line in `self`.
425
+ # You can process an IO stream character-by-character using these methods:
520
426
  #
521
- # #each_byte
522
- # : Calls the given block with each successive byte in `self` as an
523
- # integer.
427
+ # * IO#getc: Reads and returns the next character from the stream.
428
+ # * IO#readchar: Like #getc, but raises an exception at end-of-stream.
429
+ # * IO#ungetc: Pushes back ("unshifts") a character or integer onto the
430
+ # stream.
431
+ # * IO#putc: Writes a character to the stream.
432
+ # * IO#each_char: Reads each remaining character in the stream, passing the
433
+ # character to the given block.
524
434
  #
525
- # #each_char
526
- # : Calls the given block with each successive character in `self` as a
527
- # string.
435
+ # ## Byte IO
528
436
  #
529
- # #each_codepoint
530
- # : Calls the given block with each successive codepoint in `self` as an
531
- # integer.
437
+ # You can process an IO stream byte-by-byte using these methods:
532
438
  #
439
+ # * IO#getbyte: Returns the next 8-bit byte as an integer in range 0..255.
440
+ # * IO#readbyte: Like #getbyte, but raises an exception if at end-of-stream.
441
+ # * IO#ungetbyte: Pushes back ("unshifts") a byte back onto the stream.
442
+ # * IO#each_byte: Reads each remaining byte in the stream, passing the byte to
443
+ # the given block.
533
444
  #
534
445
  #
535
- # ### Settings
446
+ # ## Codepoint IO
536
447
  #
537
- # #autoclose=
538
- # : Sets whether `self` auto-closes.
448
+ # You can process an IO stream codepoint-by-codepoint:
539
449
  #
540
- # #binmode
541
- # : Sets `self` to binary mode.
450
+ # * IO#each_codepoint: Reads each remaining codepoint, passing it to the given
451
+ # block.
542
452
  #
543
- # #close
544
- # : Closes `self`.
545
453
  #
546
- # #close_on_exec=
547
- # : Sets the close-on-exec flag.
454
+ # ## What's Here
548
455
  #
549
- # #close_read
550
- # : Closes `self` for reading.
456
+ # First, what's elsewhere. Class IO:
551
457
  #
552
- # #close_write
553
- # : Closes `self` for writing.
458
+ # * Inherits from [class Object](rdoc-ref:Object@What-27s+Here).
459
+ # * Includes [module Enumerable](rdoc-ref:Enumerable@What-27s+Here), which
460
+ # provides dozens of additional methods.
554
461
  #
555
- # #set_encoding
556
- # : Sets the encoding for `self`.
557
462
  #
558
- # #set_encoding_by_bom
559
- # : Sets the encoding for `self`, based on its Unicode byte-order-mark.
463
+ # Here, class IO provides methods that are useful for:
560
464
  #
561
- # #sync=
562
- # : Sets the sync-mode to the given value.
465
+ # * [Creating](rdoc-ref:IO@Creating)
466
+ # * [Reading](rdoc-ref:IO@Reading)
467
+ # * [Writing](rdoc-ref:IO@Writing)
468
+ # * [Positioning](rdoc-ref:IO@Positioning)
469
+ # * [Iterating](rdoc-ref:IO@Iterating)
470
+ # * [Settings](rdoc-ref:IO@Settings)
471
+ # * [Querying](rdoc-ref:IO@Querying)
472
+ # * [Buffering](rdoc-ref:IO@Buffering)
473
+ # * [Low-Level Access](rdoc-ref:IO@Low-Level+Access)
474
+ # * [Other](rdoc-ref:IO@Other)
563
475
  #
564
476
  #
477
+ # ### Creating
565
478
  #
566
- # ### Querying
479
+ # * ::new (aliased as ::for_fd): Creates and returns a new IO object for the
480
+ # given integer file descriptor.
481
+ # * ::open: Creates a new IO object.
482
+ # * ::pipe: Creates a connected pair of reader and writer IO objects.
483
+ # * ::popen: Creates an IO object to interact with a subprocess.
484
+ # * ::select: Selects which given IO instances are ready for reading, writing,
485
+ # or have pending exceptions.
567
486
  #
568
- # #autoclose?
569
- # : Returns whether `self` auto-closes.
570
487
  #
571
- # #binmode?
572
- # : Returns whether `self` is in binary mode.
488
+ # ### Reading
573
489
  #
574
- # #close_on_exec?
575
- # : Returns the close-on-exec flag for `self`.
490
+ # * ::binread: Returns a binary string with all or a subset of bytes from the
491
+ # given file.
492
+ # * ::read: Returns a string with all or a subset of bytes from the given
493
+ # file.
494
+ # * ::readlines: Returns an array of strings, which are the lines from the
495
+ # given file.
496
+ # * #getbyte: Returns the next 8-bit byte read from `self` as an integer.
497
+ # * #getc: Returns the next character read from `self` as a string.
498
+ # * #gets: Returns the line read from `self`.
499
+ # * #pread: Returns all or the next *n* bytes read from `self`, not updating
500
+ # the receiver's offset.
501
+ # * #read: Returns all remaining or the next *n* bytes read from `self` for a
502
+ # given *n*.
503
+ # * #read_nonblock: the next *n* bytes read from `self` for a given *n*, in
504
+ # non-block mode.
505
+ # * #readbyte: Returns the next byte read from `self`; same as #getbyte, but
506
+ # raises an exception on end-of-stream.
507
+ # * #readchar: Returns the next character read from `self`; same as #getc, but
508
+ # raises an exception on end-of-stream.
509
+ # * #readline: Returns the next line read from `self`; same as #getline, but
510
+ # raises an exception of end-of-stream.
511
+ # * #readlines: Returns an array of all lines read read from `self`.
512
+ # * #readpartial: Returns up to the given number of bytes from `self`.
576
513
  #
577
- # #closed?
578
- # : Returns whether `self` is closed.
579
514
  #
580
- # #eof? (aliased as #eof)
581
- # : Returns whether `self` is at end-of-file.
515
+ # ### Writing
582
516
  #
583
- # #external_encoding
584
- # : Returns the external encoding object for `self`.
517
+ # * ::binwrite: Writes the given string to the file at the given filepath, in
518
+ # binary mode.
519
+ # * ::write: Writes the given string to `self`.
520
+ # * #<<: Appends the given string to `self`.
521
+ # * #print: Prints last read line or given objects to `self`.
522
+ # * #printf: Writes to `self` based on the given format string and objects.
523
+ # * #putc: Writes a character to `self`.
524
+ # * #puts: Writes lines to `self`, making sure line ends with a newline.
525
+ # * #pwrite: Writes the given string at the given offset, not updating the
526
+ # receiver's offset.
527
+ # * #write: Writes one or more given strings to `self`.
528
+ # * #write_nonblock: Writes one or more given strings to `self` in
529
+ # non-blocking mode.
585
530
  #
586
- # #fileno (aliased as #to_i)
587
- # : Returns the integer file descriptor for `self`
588
531
  #
589
- # #internal_encoding
590
- # : Returns the internal encoding object for `self`.
532
+ # ### Positioning
591
533
  #
592
- # #pid
593
- # : Returns the process ID of a child process associated with `self`, if
594
- # `self` was created by ::popen.
534
+ # * #lineno: Returns the current line number in `self`.
535
+ # * #lineno=: Sets the line number is `self`.
536
+ # * #pos (aliased as #tell): Returns the current byte offset in `self`.
537
+ # * #pos=: Sets the byte offset in `self`.
538
+ # * #reopen: Reassociates `self` with a new or existing IO stream.
539
+ # * #rewind: Positions `self` to the beginning of input.
540
+ # * #seek: Sets the offset for `self` relative to given position.
595
541
  #
596
- # #stat
597
- # : Returns the File::Stat object containing status information for
598
- # `self`.
599
542
  #
600
- # #sync
601
- # : Returns whether `self` is in sync-mode.
543
+ # ### Iterating
602
544
  #
603
- # #tty (aliased as #isatty)
604
- # : Returns whether `self` is a terminal.
545
+ # * ::foreach: Yields each line of given file to the block.
546
+ # * #each (aliased as #each_line): Calls the given block with each successive
547
+ # line in `self`.
548
+ # * #each_byte: Calls the given block with each successive byte in `self` as
549
+ # an integer.
550
+ # * #each_char: Calls the given block with each successive character in `self`
551
+ # as a string.
552
+ # * #each_codepoint: Calls the given block with each successive codepoint in
553
+ # `self` as an integer.
605
554
  #
606
555
  #
556
+ # ### Settings
607
557
  #
608
- # ### Buffering
558
+ # * #autoclose=: Sets whether `self` auto-closes.
559
+ # * #binmode: Sets `self` to binary mode.
560
+ # * #close: Closes `self`.
561
+ # * #close_on_exec=: Sets the close-on-exec flag.
562
+ # * #close_read: Closes `self` for reading.
563
+ # * #close_write: Closes `self` for writing.
564
+ # * #set_encoding: Sets the encoding for `self`.
565
+ # * #set_encoding_by_bom: Sets the encoding for `self`, based on its Unicode
566
+ # byte-order-mark.
567
+ # * #sync=: Sets the sync-mode to the given value.
609
568
  #
610
- # #fdatasync
611
- # : Immediately writes all buffered data in `self` to disk.
612
569
  #
613
- # #flush
614
- # : Flushes any buffered data within `self` to the underlying operating
615
- # system.
570
+ # ### Querying
616
571
  #
617
- # #fsync
618
- # : Immediately writes all buffered data and attributes in `self` to disk.
572
+ # * #autoclose?: Returns whether `self` auto-closes.
573
+ # * #binmode?: Returns whether `self` is in binary mode.
574
+ # * #close_on_exec?: Returns the close-on-exec flag for `self`.
575
+ # * #closed?: Returns whether `self` is closed.
576
+ # * #eof? (aliased as #eof): Returns whether `self` is at end-of-stream.
577
+ # * #external_encoding: Returns the external encoding object for `self`.
578
+ # * #fileno (aliased as #to_i): Returns the integer file descriptor for `self`
579
+ # * #internal_encoding: Returns the internal encoding object for `self`.
580
+ # * #pid: Returns the process ID of a child process associated with `self`, if
581
+ # `self` was created by ::popen.
582
+ # * #stat: Returns the File::Stat object containing status information for
583
+ # `self`.
584
+ # * #sync: Returns whether `self` is in sync-mode.
585
+ # * #tty? (aliased as #isatty): Returns whether `self` is a terminal.
619
586
  #
620
- # #ungetbyte
621
- # : Prepends buffer for `self` with given integer byte or string.
622
587
  #
623
- # #ungetc
624
- # : Prepends buffer for `self` with given string.
588
+ # ### Buffering
625
589
  #
590
+ # * #fdatasync: Immediately writes all buffered data in `self` to disk.
591
+ # * #flush: Flushes any buffered data within `self` to the underlying
592
+ # operating system.
593
+ # * #fsync: Immediately writes all buffered data and attributes in `self` to
594
+ # disk.
595
+ # * #ungetbyte: Prepends buffer for `self` with given integer byte or string.
596
+ # * #ungetc: Prepends buffer for `self` with given string.
626
597
  #
627
598
  #
628
599
  # ### Low-Level Access
629
600
  #
630
- # ::sysopen
631
- # : Opens the file given by its path, returning the integer file
632
- # descriptor.
633
- #
634
- # #advise
635
- # : Announces the intention to access data from `self` in a specific way.
636
- #
637
- # #fcntl
638
- # : Passes a low-level command to the file specified by the given file
639
- # descriptor.
640
- #
641
- # #ioctl
642
- # : Passes a low-level command to the device specified by the given file
643
- # descriptor.
644
- #
645
- # #sysread
646
- # : Returns up to the next *n* bytes read from self using a low-level
647
- # read.
648
- #
649
- # #sysseek
650
- # : Sets the offset for `self`.
651
- #
652
- # #syswrite
653
- # : Writes the given string to `self` using a low-level write.
654
- #
601
+ # * ::sysopen: Opens the file given by its path, returning the integer file
602
+ # descriptor.
603
+ # * #advise: Announces the intention to access data from `self` in a specific
604
+ # way.
605
+ # * #fcntl: Passes a low-level command to the file specified by the given file
606
+ # descriptor.
607
+ # * #ioctl: Passes a low-level command to the device specified by the given
608
+ # file descriptor.
609
+ # * #sysread: Returns up to the next *n* bytes read from self using a
610
+ # low-level read.
611
+ # * #sysseek: Sets the offset for `self`.
612
+ # * #syswrite: Writes the given string to `self` using a low-level write.
655
613
  #
656
614
  #
657
615
  # ### Other
658
616
  #
659
- # ::copy_stream
660
- # : Copies data from a source to a destination, each of which is a
661
- # filepath or an IO-like object.
662
- #
663
- # ::try_convert
664
- # : Returns a new IO object resulting from converting the given object.
665
- #
666
- # #inspect
667
- # : Returns the string representation of `self`.
617
+ # * ::copy_stream: Copies data from a source to a destination, each of which
618
+ # is a filepath or an IO-like object.
619
+ # * ::try_convert: Returns a new IO object resulting from converting the given
620
+ # object.
621
+ # * #inspect: Returns the string representation of `self`.
668
622
  #
669
623
  %a{annotate:rdoc:source:from=io.c}
670
624
  class IO < Object
@@ -677,8 +631,8 @@ class IO < Object
677
631
  # - self << object -> self
678
632
  # -->
679
633
  # Writes the given `object` to `self`, which must be opened for writing (see
680
- # [Modes](#class-IO-label-Modes)); returns `self`; if `object` is not a string,
681
- # it is converted via method `to_s`:
634
+ # [Access Modes](rdoc-ref:File@Access+Modes)); returns `self`; if `object` is
635
+ # not a string, it is converted via method `to_s`:
682
636
  #
683
637
  # $stdout << 'Hello' << ', ' << 'World!' << "\n"
684
638
  # $stdout << 'foo' << :bar << 2 << "\n"
@@ -692,56 +646,35 @@ class IO < Object
692
646
 
693
647
  # <!--
694
648
  # rdoc-file=io.c
695
- # - ios.advise(advice, offset=0, len=0) -> nil
649
+ # - advise(advice, offset = 0, len = 0) -> nil
696
650
  # -->
697
- # Announce an intention to access data from the current file in a specific
698
- # pattern. On platforms that do not support the *posix_fadvise(2)* system call,
699
- # this method is a no-op.
651
+ # Invokes Posix system call
652
+ # [posix_fadvise(2)](https://linux.die.net/man/2/posix_fadvise), which announces
653
+ # an intention to access data from the current file in a particular manner.
700
654
  #
701
- # *advice* is one of the following symbols:
655
+ # The arguments and results are platform-dependent.
702
656
  #
703
- # :normal
704
- # : No advice to give; the default assumption for an open file.
705
- # :sequential
706
- # : The data will be accessed sequentially with lower offsets read before
707
- # higher ones.
708
- # :random
709
- # : The data will be accessed in random order.
710
- # :willneed
711
- # : The data will be accessed in the near future.
712
- # :dontneed
713
- # : The data will not be accessed in the near future.
714
- # :noreuse
715
- # : The data will only be accessed once.
657
+ # The relevant data is specified by:
716
658
  #
659
+ # * `offset`: The offset of the first byte of data.
660
+ # * `len`: The number of bytes to be accessed; if `len` is zero, or is larger
661
+ # than the number of bytes remaining, all remaining bytes will be accessed.
717
662
  #
718
- # The semantics of a piece of advice are platform-dependent. See *man 2
719
- # posix_fadvise* for details.
720
663
  #
721
- # "data" means the region of the current file that begins at *offset* and
722
- # extends for *len* bytes. If *len* is 0, the region ends at the last byte of
723
- # the file. By default, both *offset* and *len* are 0, meaning that the advice
724
- # applies to the entire file.
664
+ # Argument `advice` is one of the following symbols:
725
665
  #
726
- # If an error occurs, one of the following exceptions will be raised:
666
+ # * `:normal`: The application has no advice to give about its access pattern
667
+ # for the specified data. If no advice is given for an open file, this is
668
+ # the default assumption.
669
+ # * `:sequential`: The application expects to access the specified data
670
+ # sequentially (with lower offsets read before higher ones).
671
+ # * `:random`: The specified data will be accessed in random order.
672
+ # * `:noreuse`: The specified data will be accessed only once.
673
+ # * `:willneed`: The specified data will be accessed in the near future.
674
+ # * `:dontneed`: The specified data will not be accessed in the near future.
727
675
  #
728
- # IOError
729
- # : The IO stream is closed.
730
- # Errno::EBADF
731
- # : The file descriptor of the current file is invalid.
732
- # Errno::EINVAL
733
- # : An invalid value for *advice* was given.
734
- # Errno::ESPIPE
735
- # : The file descriptor of the current file refers to a FIFO or pipe. (Linux
736
- # raises Errno::EINVAL in this case).
737
- # TypeError
738
- # : Either *advice* was not a Symbol, or one of the other arguments was not an
739
- # Integer.
740
- # RangeError
741
- # : One of the arguments given was too big/small.
742
676
  #
743
- # This list is not exhaustive; other Errno
744
- # : exceptions are also possible.
677
+ # Not implemented on all platforms.
745
678
  #
746
679
  def advise: (:normal | :sequential | :random | :willneed | :dontneed | :noreuse advise, ?Integer offset, ?Integer len) -> nil
747
680
 
@@ -774,43 +707,60 @@ class IO < Object
774
707
 
775
708
  # <!--
776
709
  # rdoc-file=io.c
777
- # - ios.binmode -> ios
710
+ # - binmode -> self
778
711
  # -->
779
- # Puts *ios* into binary mode. Once a stream is in binary mode, it cannot be
780
- # reset to nonbinary mode.
712
+ # Sets the stream's data mode as binary (see [Data
713
+ # Mode](rdoc-ref:File@Data+Mode)).
781
714
  #
782
- # * newline conversion disabled
783
- # * encoding conversion disabled
784
- # * content is treated as ASCII-8BIT
715
+ # A stream's data mode may not be changed from binary to text.
785
716
  #
786
717
  def binmode: () -> self
787
718
 
788
719
  # <!--
789
720
  # rdoc-file=io.c
790
- # - ios.binmode? -> true or false
721
+ # - binmode? -> true or false
791
722
  # -->
792
- # Returns `true` if *ios* is binmode.
723
+ # Returns `true` if the stream is on binary mode, `false` otherwise. See [Data
724
+ # Mode](rdoc-ref:File@Data+Mode).
793
725
  #
794
726
  def binmode?: () -> bool
795
727
 
796
728
  # <!--
797
729
  # rdoc-file=io.c
798
- # - ios.close -> nil
730
+ # - close -> nil
799
731
  # -->
800
- # Closes *ios* and flushes any pending writes to the operating system. The
801
- # stream is unavailable for any further data operations; an IOError is raised if
802
- # such an attempt is made. I/O streams are automatically closed when they are
803
- # claimed by the garbage collector.
732
+ # Closes the stream for both reading and writing if open for either or both;
733
+ # returns `nil`. See [Open and Closed
734
+ # Streams](rdoc-ref:IO@Open+and+Closed+Streams).
735
+ #
736
+ # If the stream is open for writing, flushes any buffered writes to the
737
+ # operating system before closing.
804
738
  #
805
- # If *ios* is opened by IO.popen, #close sets `$?`.
739
+ # If the stream was opened by IO.popen, sets global variable `$?` (child exit
740
+ # status).
806
741
  #
807
- # Calling this method on closed IO object is just ignored since Ruby 2.3.
742
+ # Example:
743
+ #
744
+ # IO.popen('ruby', 'r+') do |pipe|
745
+ # puts pipe.closed?
746
+ # pipe.close
747
+ # puts $?
748
+ # puts pipe.closed?
749
+ # end
750
+ #
751
+ # Output:
752
+ #
753
+ # false
754
+ # pid 13760 exit 0
755
+ # true
756
+ #
757
+ # Related: IO#close_read, IO#close_write, IO#closed?.
808
758
  #
809
759
  def close: () -> NilClass
810
760
 
811
761
  # <!--
812
762
  # rdoc-file=io.c
813
- # - ios.close_on_exec = bool -> true or false
763
+ # - self.close_on_exec = bool -> true or false
814
764
  # -->
815
765
  # Sets a close-on-exec flag.
816
766
  #
@@ -829,157 +779,272 @@ class IO < Object
829
779
 
830
780
  # <!--
831
781
  # rdoc-file=io.c
832
- # - ios.close_on_exec? -> true or false
782
+ # - close_on_exec? -> true or false
833
783
  # -->
834
- # Returns `true` if *ios* will be closed on exec.
784
+ # Returns `true` if the stream will be closed on exec, `false` otherwise:
835
785
  #
836
- # f = open("/dev/null")
837
- # f.close_on_exec? #=> false
838
- # f.close_on_exec = true
839
- # f.close_on_exec? #=> true
786
+ # f = File.open('t.txt')
787
+ # f.close_on_exec? # => true
840
788
  # f.close_on_exec = false
841
- # f.close_on_exec? #=> false
789
+ # f.close_on_exec? # => false
790
+ # f.close
842
791
  #
843
792
  def close_on_exec?: () -> bool
844
793
 
845
794
  # <!--
846
795
  # rdoc-file=io.c
847
- # - ios.close_read -> nil
796
+ # - close_read -> nil
848
797
  # -->
849
- # Closes the read end of a duplex I/O stream (i.e., one that contains both a
850
- # read and a write stream, such as a pipe). Will raise an IOError if the stream
851
- # is not duplexed.
798
+ # Closes the stream for reading if open for reading; returns `nil`. See [Open
799
+ # and Closed Streams](rdoc-ref:IO@Open+and+Closed+Streams).
852
800
  #
853
- # f = IO.popen("/bin/sh","r+")
854
- # f.close_read
855
- # f.readlines
801
+ # If the stream was opened by IO.popen and is also closed for writing, sets
802
+ # global variable `$?` (child exit status).
856
803
  #
857
- # *produces:*
804
+ # Example:
858
805
  #
859
- # prog.rb:3:in `readlines': not opened for reading (IOError)
860
- # from prog.rb:3
806
+ # IO.popen('ruby', 'r+') do |pipe|
807
+ # puts pipe.closed?
808
+ # pipe.close_write
809
+ # puts pipe.closed?
810
+ # pipe.close_read
811
+ # puts $?
812
+ # puts pipe.closed?
813
+ # end
861
814
  #
862
- # Calling this method on closed IO object is just ignored since Ruby 2.3.
815
+ # Output:
816
+ #
817
+ # false
818
+ # false
819
+ # pid 14748 exit 0
820
+ # true
821
+ #
822
+ # Related: IO#close, IO#close_write, IO#closed?.
863
823
  #
864
824
  def close_read: () -> NilClass
865
825
 
866
826
  # <!--
867
827
  # rdoc-file=io.c
868
- # - ios.close_write -> nil
828
+ # - close_write -> nil
869
829
  # -->
870
- # Closes the write end of a duplex I/O stream (i.e., one that contains both a
871
- # read and a write stream, such as a pipe). Will raise an IOError if the stream
872
- # is not duplexed.
830
+ # Closes the stream for writing if open for writing; returns `nil`. See [Open
831
+ # and Closed Streams](rdoc-ref:IO@Open+and+Closed+Streams).
873
832
  #
874
- # f = IO.popen("/bin/sh","r+")
875
- # f.close_write
876
- # f.print "nowhere"
833
+ # Flushes any buffered writes to the operating system before closing.
877
834
  #
878
- # *produces:*
835
+ # If the stream was opened by IO.popen and is also closed for reading, sets
836
+ # global variable `$?` (child exit status).
879
837
  #
880
- # prog.rb:3:in `write': not opened for writing (IOError)
881
- # from prog.rb:3:in `print'
882
- # from prog.rb:3
838
+ # IO.popen('ruby', 'r+') do |pipe|
839
+ # puts pipe.closed?
840
+ # pipe.close_read
841
+ # puts pipe.closed?
842
+ # pipe.close_write
843
+ # puts $?
844
+ # puts pipe.closed?
845
+ # end
883
846
  #
884
- # Calling this method on closed IO object is just ignored since Ruby 2.3.
847
+ # Output:
848
+ #
849
+ # false
850
+ # false
851
+ # pid 15044 exit 0
852
+ # true
853
+ #
854
+ # Related: IO#close, IO#close_read, IO#closed?.
885
855
  #
886
856
  def close_write: () -> NilClass
887
857
 
888
858
  # <!--
889
859
  # rdoc-file=io.c
890
- # - ios.closed? -> true or false
860
+ # - closed? -> true or false
891
861
  # -->
892
- # Returns `true` if *ios* is completely closed (for duplex streams, both reader
893
- # and writer), `false` otherwise.
862
+ # Returns `true` if the stream is closed for both reading and writing, `false`
863
+ # otherwise. See [Open and Closed Streams](rdoc-ref:IO@Open+and+Closed+Streams).
894
864
  #
895
- # f = File.new("testfile")
896
- # f.close #=> nil
897
- # f.closed? #=> true
898
- # f = IO.popen("/bin/sh","r+")
899
- # f.close_write #=> nil
900
- # f.closed? #=> false
901
- # f.close_read #=> nil
902
- # f.closed? #=> true
865
+ # IO.popen('ruby', 'r+') do |pipe|
866
+ # puts pipe.closed?
867
+ # pipe.close_read
868
+ # puts pipe.closed?
869
+ # pipe.close_write
870
+ # puts pipe.closed?
871
+ # end
872
+ #
873
+ # Output:
874
+ #
875
+ # false
876
+ # false
877
+ # true
878
+ #
879
+ # Related: IO#close_read, IO#close_write, IO#close.
903
880
  #
904
881
  def closed?: () -> bool
905
882
 
906
883
  # <!--
907
884
  # rdoc-file=io.c
908
- # - ios.each(sep=$/ [, getline_args]) {|line| block } -> ios
909
- # - ios.each(limit [, getline_args]) {|line| block } -> ios
910
- # - ios.each(sep, limit [, getline_args]) {|line| block } -> ios
911
- # - ios.each(...) -> an_enumerator
912
- # - ios.each_line(sep=$/ [, getline_args]) {|line| block } -> ios
913
- # - ios.each_line(limit [, getline_args]) {|line| block } -> ios
914
- # - ios.each_line(sep, limit [, getline_args]) {|line| block } -> ios
915
- # - ios.each_line(...) -> an_enumerator
885
+ # - each_line(sep = $/, chomp: false) {|line| ... } -> self
886
+ # - each_line(limit, chomp: false) {|line| ... } -> self
887
+ # - each_line(sep, limit, chomp: false) {|line| ... } -> self
888
+ # - each_line -> enumerator
916
889
  # -->
917
- # Executes the block for every line in *ios*, where lines are separated by
918
- # *sep*. *ios* must be opened for reading or an IOError will be raised.
890
+ # Calls the block with each remaining line read from the stream; returns `self`.
891
+ # Does nothing if already at end-of-stream; See [Line IO](rdoc-ref:IO@Line+IO).
919
892
  #
920
- # If no block is given, an enumerator is returned instead.
893
+ # With no arguments given, reads lines as determined by line separator `$/`:
921
894
  #
922
- # f = File.new("testfile")
923
- # f.each {|line| puts "#{f.lineno}: #{line}" }
895
+ # f = File.new('t.txt')
896
+ # f.each_line {|line| p line }
897
+ # f.each_line {|line| fail 'Cannot happen' }
898
+ # f.close
924
899
  #
925
- # *produces:*
900
+ # Output:
901
+ #
902
+ # "First line\n"
903
+ # "Second line\n"
904
+ # "\n"
905
+ # "Fourth line\n"
906
+ # "Fifth line\n"
907
+ #
908
+ # With only string argument `sep` given, reads lines as determined by line
909
+ # separator `sep`; see [Line Separator](rdoc-ref:IO@Line+Separator):
926
910
  #
927
- # 1: This is line one
928
- # 2: This is line two
929
- # 3: This is line three
930
- # 4: And so on...
911
+ # f = File.new('t.txt')
912
+ # f.each_line('li') {|line| p line }
913
+ # f.close
914
+ #
915
+ # Output:
916
+ #
917
+ # "First li"
918
+ # "ne\nSecond li"
919
+ # "ne\n\nFourth li"
920
+ # "ne\nFifth li"
921
+ # "ne\n"
922
+ #
923
+ # The two special values for `sep` are honored:
924
+ #
925
+ # f = File.new('t.txt')
926
+ # # Get all into one string.
927
+ # f.each_line(nil) {|line| p line }
928
+ # f.close
929
+ #
930
+ # Output:
931
931
  #
932
- # See IO.readlines for details about getline_args.
932
+ # "First line\nSecond line\n\nFourth line\nFifth line\n"
933
+ #
934
+ # f.rewind
935
+ # # Get paragraphs (up to two line separators).
936
+ # f.each_line('') {|line| p line }
937
+ #
938
+ # Output:
939
+ #
940
+ # "First line\nSecond line\n\n"
941
+ # "Fourth line\nFifth line\n"
942
+ #
943
+ # With only integer argument `limit` given, limits the number of bytes in each
944
+ # line; see [Line Limit](rdoc-ref:IO@Line+Limit):
945
+ #
946
+ # f = File.new('t.txt')
947
+ # f.each_line(8) {|line| p line }
948
+ # f.close
949
+ #
950
+ # Output:
951
+ #
952
+ # "First li"
953
+ # "ne\n"
954
+ # "Second l"
955
+ # "ine\n"
956
+ # "\n"
957
+ # "Fourth l"
958
+ # "ine\n"
959
+ # "Fifth li"
960
+ # "ne\n"
961
+ #
962
+ # With arguments `sep` and `limit` given, combines the two behaviors:
963
+ #
964
+ # * Calls with the next line as determined by line separator `sep`.
965
+ # * But returns no more bytes than are allowed by the limit.
966
+ #
967
+ #
968
+ # Optional keyword argument `chomp` specifies whether line separators are to be
969
+ # omitted:
970
+ #
971
+ # f = File.new('t.txt')
972
+ # f.each_line(chomp: true) {|line| p line }
973
+ # f.close
974
+ #
975
+ # Output:
976
+ #
977
+ # "First line"
978
+ # "Second line"
979
+ # ""
980
+ # "Fourth line"
981
+ # "Fifth line"
982
+ #
983
+ # Returns an Enumerator if no block is given.
984
+ #
985
+ # IO#each is an alias for IO#each_line.
933
986
  #
934
987
  def each: (?String sep, ?Integer limit) { (String arg0) -> untyped } -> self
935
988
  | (?String sep, ?Integer limit) -> ::Enumerator[String, self]
936
989
 
937
990
  # <!--
938
991
  # rdoc-file=io.c
939
- # - ios.each_byte {|byte| block } -> ios
940
- # - ios.each_byte -> an_enumerator
992
+ # - each_byte {|byte| ... } -> self
993
+ # - each_byte -> enumerator
941
994
  # -->
942
- # Calls the given block once for each byte (0..255) in *ios*, passing the byte
943
- # as an argument. The stream must be opened for reading or an IOError will be
944
- # raised.
995
+ # Calls the given block with each byte (0..255) in the stream; returns `self`.
996
+ # See [Byte IO](rdoc-ref:IO@Byte+IO).
945
997
  #
946
- # If no block is given, an enumerator is returned instead.
998
+ # f = File.new('t.rus')
999
+ # a = []
1000
+ # f.each_byte {|b| a << b }
1001
+ # a # => [209, 130, 208, 181, 209, 129, 209, 130]
1002
+ # f.close
947
1003
  #
948
- # f = File.new("testfile")
949
- # checksum = 0
950
- # f.each_byte {|x| checksum ^= x } #=> #<File:testfile>
951
- # checksum #=> 12
1004
+ # Returns an Enumerator if no block is given.
1005
+ #
1006
+ # Related: IO#each_char, IO#each_codepoint.
952
1007
  #
953
1008
  def each_byte: () { (Integer arg0) -> untyped } -> self
954
1009
  | () -> ::Enumerator[Integer, self]
955
1010
 
956
1011
  # <!--
957
1012
  # rdoc-file=io.c
958
- # - ios.each_char {|c| block } -> ios
959
- # - ios.each_char -> an_enumerator
1013
+ # - each_char {|c| ... } -> self
1014
+ # - each_char -> enumerator
960
1015
  # -->
961
- # Calls the given block once for each character in *ios*, passing the character
962
- # as an argument. The stream must be opened for reading or an IOError will be
963
- # raised.
1016
+ # Calls the given block with each character in the stream; returns `self`. See
1017
+ # [Character IO](rdoc-ref:IO@Character+IO).
964
1018
  #
965
- # If no block is given, an enumerator is returned instead.
1019
+ # f = File.new('t.rus')
1020
+ # a = []
1021
+ # f.each_char {|c| a << c.ord }
1022
+ # a # => [1090, 1077, 1089, 1090]
1023
+ # f.close
966
1024
  #
967
- # f = File.new("testfile")
968
- # f.each_char {|c| print c, ' ' } #=> #<File:testfile>
1025
+ # Returns an Enumerator if no block is given.
1026
+ #
1027
+ # Related: IO#each_byte, IO#each_codepoint.
969
1028
  #
970
1029
  def each_char: () { (String arg0) -> untyped } -> self
971
1030
  | () -> ::Enumerator[String, self]
972
1031
 
973
1032
  # <!--
974
1033
  # rdoc-file=io.c
975
- # - ios.each_codepoint {|c| block } -> ios
976
- # - ios.each_codepoint -> an_enumerator
1034
+ # - each_codepoint {|c| ... } -> self
1035
+ # - each_codepoint -> enumerator
977
1036
  # -->
978
- # Passes the Integer ordinal of each character in *ios*, passing the codepoint
979
- # as an argument. The stream must be opened for reading or an IOError will be
980
- # raised.
1037
+ # Calls the given block with each codepoint in the stream; returns `self`:
1038
+ #
1039
+ # f = File.new('t.rus')
1040
+ # a = []
1041
+ # f.each_codepoint {|c| a << c }
1042
+ # a # => [1090, 1077, 1089, 1090]
1043
+ # f.close
981
1044
  #
982
- # If no block is given, an enumerator is returned instead.
1045
+ # Returns an Enumerator if no block is given.
1046
+ #
1047
+ # Related: IO#each_byte, IO#each_char.
983
1048
  #
984
1049
  def each_codepoint: () { (Integer arg0) -> untyped } -> self
985
1050
  | () -> ::Enumerator[Integer, self]
@@ -989,15 +1054,16 @@ class IO < Object
989
1054
  # - eof -> true or false
990
1055
  # -->
991
1056
  # Returns `true` if the stream is positioned at its end, `false` otherwise; see
992
- # [Position](#class-IO-label-Position):
1057
+ # [Position](rdoc-ref:IO@Position):
993
1058
  #
994
1059
  # f = File.open('t.txt')
995
1060
  # f.eof # => false
996
1061
  # f.seek(0, :END) # => 0
997
1062
  # f.eof # => true
1063
+ # f.close
998
1064
  #
999
1065
  # Raises an exception unless the stream is opened for reading; see
1000
- # [Mode](#class-IO-label-Mode).
1066
+ # [Mode](rdoc-ref:File@Access+Modes).
1001
1067
  #
1002
1068
  # If `self` is a stream such as pipe or socket, this method blocks until the
1003
1069
  # other end sends some data or closes it:
@@ -1017,20 +1083,23 @@ class IO < Object
1017
1083
  # not behave as you intend with IO#eof?, unless you call IO#rewind first (which
1018
1084
  # is not available for some streams).
1019
1085
  #
1020
- # I#eof? is an alias for IO#eof.
1086
+ # IO#eof? is an alias for IO#eof.
1021
1087
  #
1022
1088
  def eof: () -> bool
1023
1089
 
1024
1090
  # <!--
1025
1091
  # rdoc-file=io.c
1026
- # - ios.fcntl(integer_cmd, arg) -> integer
1092
+ # - fcntl(integer_cmd, argument) -> integer
1027
1093
  # -->
1028
- # Provides a mechanism for issuing low-level commands to control or query
1029
- # file-oriented I/O streams. Arguments and results are platform dependent. If
1030
- # *arg* is a number, its value is passed directly. If it is a string, it is
1031
- # interpreted as a binary sequence of bytes (Array#pack might be a useful way to
1032
- # build this string). On Unix platforms, see `fcntl(2)` for details. Not
1033
- # implemented on all platforms.
1094
+ # Invokes Posix system call [fcntl(2)](https://linux.die.net/man/2/fcntl), which
1095
+ # provides a mechanism for issuing low-level commands to control or query a
1096
+ # file-oriented I/O stream. Arguments and results are platform dependent.
1097
+ #
1098
+ # If +argument is a number, its value is passed directly; if it is a string, it
1099
+ # is interpreted as a binary sequence of bytes. (Array#pack might be a useful
1100
+ # way to build this string.)
1101
+ #
1102
+ # Not implemented on all platforms.
1034
1103
  #
1035
1104
  def fcntl: (Integer integer_cmd, String | Integer arg) -> Integer
1036
1105
 
@@ -1054,6 +1123,7 @@ class IO < Object
1054
1123
  # $stdout.fileno # => 1
1055
1124
  # $stderr.fileno # => 2
1056
1125
  # File.open('t.txt').fileno # => 10
1126
+ # f.close
1057
1127
  #
1058
1128
  # IO#to_i is an alias for IO#fileno.
1059
1129
  #
@@ -1093,256 +1163,153 @@ class IO < Object
1093
1163
 
1094
1164
  # <!--
1095
1165
  # rdoc-file=io.c
1096
- # - ios.getbyte -> integer or nil
1166
+ # - getbyte -> integer or nil
1097
1167
  # -->
1098
- # Gets the next 8-bit byte (0..255) from *ios*. Returns `nil` if called at end
1099
- # of file.
1168
+ # Reads and returns the next byte (in range 0..255) from the stream; returns
1169
+ # `nil` if already at end-of-stream. See [Byte IO](rdoc-ref:IO@Byte+IO).
1100
1170
  #
1101
- # f = File.new("testfile")
1102
- # f.getbyte #=> 84
1103
- # f.getbyte #=> 104
1171
+ # f = File.open('t.txt')
1172
+ # f.getbyte # => 70
1173
+ # f.close
1174
+ # f = File.open('t.rus')
1175
+ # f.getbyte # => 209
1176
+ # f.close
1177
+ #
1178
+ # Related: IO#readbyte (may raise EOFError).
1104
1179
  #
1105
1180
  def getbyte: () -> Integer?
1106
1181
 
1107
1182
  # <!--
1108
1183
  # rdoc-file=io.c
1109
- # - ios.getc -> string or nil
1184
+ # - getc -> character or nil
1110
1185
  # -->
1111
- # Reads a one-character string from *ios*. Returns `nil` if called at end of
1112
- # file.
1186
+ # Reads and returns the next 1-character string from the stream; returns `nil`
1187
+ # if already at end-of-stream. See [Character IO](rdoc-ref:IO@Character+IO).
1113
1188
  #
1114
- # f = File.new("testfile")
1115
- # f.getc #=> "h"
1116
- # f.getc #=> "e"
1189
+ # f = File.open('t.txt')
1190
+ # f.getc # => "F"
1191
+ # f.close
1192
+ # f = File.open('t.rus')
1193
+ # f.getc.ord # => 1090
1194
+ # f.close
1195
+ #
1196
+ # Related: IO#readchar (may raise EOFError).
1117
1197
  #
1118
1198
  def getc: () -> String?
1119
1199
 
1120
1200
  # <!--
1121
1201
  # rdoc-file=io.c
1122
- # - gets(sep = $/, **getline_opts) -> string or nil
1123
- # - gets(limit, **getline_opts) -> string or nil
1124
- # - gets(sep, limit, **getline_opts) -> string or nil
1202
+ # - gets(sep = $/, chomp: false) -> string or nil
1203
+ # - gets(limit, chomp: false) -> string or nil
1204
+ # - gets(sep, limit, chomp: false) -> string or nil
1125
1205
  # -->
1126
- # Reads and returns data from the stream; assigns the return value to `$_`.
1206
+ # Reads and returns a line from the stream; assigns the return value to `$_`.
1207
+ # See [Line IO](rdoc-ref:IO@Line+IO).
1127
1208
  #
1128
1209
  # With no arguments given, returns the next line as determined by line separator
1129
1210
  # `$/`, or `nil` if none:
1130
1211
  #
1131
1212
  # f = File.open('t.txt')
1132
- # f.gets # => "This is line one.\n"
1133
- # $_ # => "This is line one.\n"
1134
- # f.gets # => "This is the second line.\n"
1135
- # f.gets # => "This is the third line.\n"
1213
+ # f.gets # => "First line\n"
1214
+ # $_ # => "First line\n"
1215
+ # f.gets # => "\n"
1216
+ # f.gets # => "Fourth line\n"
1217
+ # f.gets # => "Fifth line\n"
1136
1218
  # f.gets # => nil
1219
+ # f.close
1137
1220
  #
1138
- # With string argument `sep` given, but not argument `limit`, returns the next
1139
- # line as determined by line separator `sep`, or `nil` if none:
1140
- #
1141
- # f = File.open('t.txt')
1142
- # f.gets(' is') # => "This is"
1143
- # f.gets(' is') # => " line one.\nThis is"
1144
- # f.gets(' is') # => " the second line.\nThis is"
1145
- # f.gets(' is') # => " the third line.\n"
1146
- # f.gets(' is') # => nil
1221
+ # With only string argument `sep` given, returns the next line as determined by
1222
+ # line separator `sep`, or `nil` if none; see [Line
1223
+ # Separator](rdoc-ref:IO@Line+Separator):
1147
1224
  #
1148
- # Note two special values for `sep`:
1225
+ # f = File.new('t.txt')
1226
+ # f.gets('l') # => "First l"
1227
+ # f.gets('li') # => "ine\nSecond li"
1228
+ # f.gets('lin') # => "ne\n\nFourth lin"
1229
+ # f.gets # => "e\n"
1230
+ # f.close
1149
1231
  #
1150
- # * `nil`: The entire stream is read and returned.
1151
- # * `''` (empty string): The next "paragraph" is read and returned, the
1152
- # paragraph separator being two successive line separators.
1232
+ # The two special values for `sep` are honored:
1153
1233
  #
1234
+ # f = File.new('t.txt')
1235
+ # # Get all.
1236
+ # f.gets(nil) # => "First line\nSecond line\n\nFourth line\nFifth line\n"
1237
+ # f.rewind
1238
+ # # Get paragraph (up to two line separators).
1239
+ # f.gets('') # => "First line\nSecond line\n\n"
1240
+ # f.close
1154
1241
  #
1155
- # With integer argument `limit` given, returns up to `limit+1` bytes:
1242
+ # With only integer argument `limit` given, limits the number of bytes in the
1243
+ # line; see [Line Limit](rdoc-ref:IO@Line+Limit):
1156
1244
  #
1157
- # # Text with 1-byte characters.
1158
- # File.open('t.txt') {|f| f.gets(1) } # => "T"
1159
- # File.open('t.txt') {|f| f.gets(2) } # => "Th"
1160
- # File.open('t.txt') {|f| f.gets(3) } # => "Thi"
1161
- # File.open('t.txt') {|f| f.gets(4) } # => "This"
1162
1245
  # # No more than one line.
1163
- # File.open('t.txt') {|f| f.gets(17) } # => "This is line one."
1164
- # File.open('t.txt') {|f| f.gets(18) } # => "This is line one.\n"
1165
- # File.open('t.txt') {|f| f.gets(19) } # => "This is line one.\n"
1166
- #
1167
- # # Text with 2-byte characters, which will not be split.
1168
- # File.open('t.rus') {|f| f.gets(1).size } # => 1
1169
- # File.open('t.rus') {|f| f.gets(2).size } # => 1
1170
- # File.open('t.rus') {|f| f.gets(3).size } # => 2
1171
- # File.open('t.rus') {|f| f.gets(4).size } # => 2
1246
+ # File.open('t.txt') {|f| f.gets(10) } # => "First line"
1247
+ # File.open('t.txt') {|f| f.gets(11) } # => "First line\n"
1248
+ # File.open('t.txt') {|f| f.gets(12) } # => "First line\n"
1172
1249
  #
1173
- # With arguments `sep` and `limit`, combines the two behaviors above:
1250
+ # With arguments `sep` and `limit` given, combines the two behaviors:
1174
1251
  #
1175
1252
  # * Returns the next line as determined by line separator `sep`, or `nil` if
1176
1253
  # none.
1177
- # * But returns no more than `limit+1` bytes.
1254
+ # * But returns no more bytes than are allowed by the limit.
1178
1255
  #
1179
1256
  #
1180
- # For all forms above, trailing optional keyword arguments may be given; see
1181
- # [Getline Options](#class-IO-label-Getline+Options):
1257
+ # Optional keyword argument `chomp` specifies whether line separators are to be
1258
+ # omitted:
1182
1259
  #
1183
1260
  # f = File.open('t.txt')
1184
1261
  # # Chomp the lines.
1185
- # f.gets(chomp: true) # => "This is line one."
1186
- # f.gets(chomp: true) # => "This is the second line."
1187
- # f.gets(chomp: true) # => "This is the third line."
1262
+ # f.gets(chomp: true) # => "First line"
1263
+ # f.gets(chomp: true) # => "Second line"
1264
+ # f.gets(chomp: true) # => ""
1265
+ # f.gets(chomp: true) # => "Fourth line"
1266
+ # f.gets(chomp: true) # => "Fifth line"
1188
1267
  # f.gets(chomp: true) # => nil
1268
+ # f.close
1189
1269
  #
1190
1270
  def gets: (?String sep, ?Integer limit) -> String?
1191
1271
 
1192
1272
  # <!--
1193
1273
  # rdoc-file=io.c
1194
- # - IO.new(fd [, mode] [, opt]) -> io
1274
+ # - IO.new(fd, mode = 'r', **opts) -> io
1195
1275
  # -->
1196
- # Returns a new IO object (a stream) for the given integer file descriptor `fd`
1197
- # and `mode` string. `opt` may be used to specify parts of `mode` in a more
1198
- # readable fashion. See also IO.sysopen and IO.for_fd.
1199
- #
1200
- # IO.new is called by various File and IO opening methods such as IO::open,
1201
- # Kernel#open, and File::open.
1202
- #
1203
- # ### Open Mode
1204
- #
1205
- # When `mode` is an integer it must be combination of the modes defined in
1206
- # File::Constants (`File::RDONLY`, `File::WRONLY|File::CREAT`). See the open(2)
1207
- # man page for more information.
1208
- #
1209
- # When `mode` is a string it must be in one of the following forms:
1210
- #
1211
- # fmode
1212
- # fmode ":" ext_enc
1213
- # fmode ":" ext_enc ":" int_enc
1214
- # fmode ":" "BOM|UTF-*"
1215
- #
1216
- # `fmode` is an IO open mode string, `ext_enc` is the external encoding for the
1217
- # IO and `int_enc` is the internal encoding.
1218
- #
1219
- # #### IO Open Mode
1220
- #
1221
- # Ruby allows the following open modes:
1222
- #
1223
- # "r" Read-only, starts at beginning of file (default mode).
1224
- #
1225
- # "r+" Read-write, starts at beginning of file.
1226
- #
1227
- # "w" Write-only, truncates existing file
1228
- # to zero length or creates a new file for writing.
1229
- #
1230
- # "w+" Read-write, truncates existing file to zero length
1231
- # or creates a new file for reading and writing.
1232
- #
1233
- # "a" Write-only, each write call appends data at end of file.
1234
- # Creates a new file for writing if file does not exist.
1235
- #
1236
- # "a+" Read-write, each write call appends data at end of file.
1237
- # Creates a new file for reading and writing if file does
1238
- # not exist.
1239
- #
1240
- # The following modes must be used separately, and along with one or more of the
1241
- # modes seen above.
1242
- #
1243
- # "b" Binary file mode
1244
- # Suppresses EOL <-> CRLF conversion on Windows. And
1245
- # sets external encoding to ASCII-8BIT unless explicitly
1246
- # specified.
1247
- #
1248
- # "t" Text file mode
1276
+ # Creates and returns a new IO object (file stream) from a file descriptor.
1249
1277
  #
1250
- # The exclusive access mode ("x") can be used together with "w" to ensure the
1251
- # file is created. Errno::EEXIST is raised when it already exists. It may not be
1252
- # supported with all kinds of streams (e.g. pipes).
1278
+ # IO.new may be useful for interaction with low-level libraries. For
1279
+ # higher-level interactions, it may be simpler to create the file stream using
1280
+ # File.open.
1253
1281
  #
1254
- # When the open mode of original IO is read only, the mode cannot be changed to
1255
- # be writable. Similarly, the open mode cannot be changed from write only to
1256
- # readable.
1282
+ # Argument `fd` must be a valid file descriptor (integer):
1257
1283
  #
1258
- # When such a change is attempted the error is raised in different locations
1259
- # according to the platform.
1284
+ # path = 't.tmp'
1285
+ # fd = IO.sysopen(path) # => 3
1286
+ # IO.new(fd) # => #<IO:fd 3>
1260
1287
  #
1261
- # ### IO Encoding
1288
+ # The new IO object does not inherit encoding (because the integer file
1289
+ # descriptor does not have an encoding):
1262
1290
  #
1263
- # When `ext_enc` is specified, strings read will be tagged by the encoding when
1264
- # reading, and strings output will be converted to the specified encoding when
1265
- # writing.
1291
+ # fd = IO.sysopen('t.rus', 'rb')
1292
+ # io = IO.new(fd)
1293
+ # io.external_encoding # => #<Encoding:UTF-8> # Not ASCII-8BIT.
1266
1294
  #
1267
- # When `ext_enc` and `int_enc` are specified read strings will be converted from
1268
- # `ext_enc` to `int_enc` upon input, and written strings will be converted from
1269
- # `int_enc` to `ext_enc` upon output. See Encoding for further details of
1270
- # transcoding on input and output.
1295
+ # Optional argument `mode` (defaults to 'r') must specify a valid mode; see
1296
+ # [Access Modes](rdoc-ref:File@Access+Modes):
1271
1297
  #
1272
- # If "BOM|UTF-8", "BOM|UTF-16LE" or "BOM|UTF16-BE" are used, Ruby checks for a
1273
- # Unicode BOM in the input document to help determine the encoding. For UTF-16
1274
- # encodings the file open mode must be binary. When present, the BOM is
1275
- # stripped and the external encoding from the BOM is used. When the BOM is
1276
- # missing the given Unicode encoding is used as `ext_enc`. (The BOM-set
1277
- # encoding option is case insensitive, so "bom|utf-8" is also valid.)
1298
+ # IO.new(fd, 'w') # => #<IO:fd 3>
1299
+ # IO.new(fd, File::WRONLY) # => #<IO:fd 3>
1278
1300
  #
1279
- # ### Options
1301
+ # Optional keyword arguments `opts` specify:
1280
1302
  #
1281
- # `opt` can be used instead of `mode` for improved readability. The following
1282
- # keys are supported:
1303
+ # * [Open Options](rdoc-ref:IO@Open+Options).
1304
+ # * [Encoding options](rdoc-ref:encodings.rdoc@Encoding+Options).
1283
1305
  #
1284
- # :mode
1285
- # : Same as `mode` parameter
1286
1306
  #
1287
- # :flags
1288
- # : Specifies file open flags as integer. If `mode` parameter is given, this
1289
- # parameter will be bitwise-ORed.
1290
- #
1291
- # :external_encoding
1292
- # : External encoding for the IO.
1293
- #
1294
- # :internal_encoding
1295
- # : Internal encoding for the IO. "-" is a synonym for the default internal
1296
- # encoding.
1297
- #
1298
- # If the value is `nil` no conversion occurs.
1299
- #
1300
- # :encoding
1301
- # : Specifies external and internal encodings as "extern:intern".
1302
- #
1303
- # :textmode
1304
- # : If the value is truth value, same as "t" in argument `mode`.
1305
- #
1306
- # :binmode
1307
- # : If the value is truth value, same as "b" in argument `mode`.
1308
- #
1309
- # :autoclose
1310
- # : If the value is `false`, the `fd` will be kept open after this IO instance
1311
- # gets finalized.
1312
- #
1313
- #
1314
- # Also, `opt` can have same keys in String#encode for controlling conversion
1315
- # between the external encoding and the internal encoding.
1316
- #
1317
- # ### Example 1
1318
- #
1319
- # fd = IO.sysopen("/dev/tty", "w")
1320
- # a = IO.new(fd,"w")
1321
- # $stderr.puts "Hello"
1322
- # a.puts "World"
1323
- #
1324
- # Produces:
1325
- #
1326
- # Hello
1327
- # World
1328
- #
1329
- # ### Example 2
1330
- #
1331
- # require 'fcntl'
1332
- #
1333
- # fd = STDERR.fcntl(Fcntl::F_DUPFD)
1334
- # io = IO.new(fd, mode: 'w:UTF-16LE', cr_newline: true)
1335
- # io.puts "Hello, World!"
1336
- #
1337
- # fd = STDERR.fcntl(Fcntl::F_DUPFD)
1338
- # io = IO.new(fd, mode: 'w', cr_newline: true,
1339
- # external_encoding: Encoding::UTF_16LE)
1340
- # io.puts "Hello, World!"
1307
+ # Examples:
1341
1308
  #
1342
- # Both of above print "Hello, World!" in UTF-16LE to standard error output with
1343
- # converting EOL generated by #puts to CR.
1309
+ # IO.new(fd, internal_encoding: nil) # => #<IO:fd 3>
1310
+ # IO.new(fd, autoclose: true) # => #<IO:fd 3>
1344
1311
  #
1345
- def initialize: (Integer fd, ?Integer mode, ?Integer opt) -> void
1312
+ def initialize: (int fd, ?string | int mode, ?path: string?, **untyped opts) -> void
1346
1313
 
1347
1314
  # <!--
1348
1315
  # rdoc-file=io.c
@@ -1352,84 +1319,86 @@ class IO < Object
1352
1319
  #
1353
1320
  # f = File.open('t.txt')
1354
1321
  # f.inspect # => "#<File:t.txt>"
1322
+ # f.close
1355
1323
  #
1356
1324
  def inspect: () -> String
1357
1325
 
1358
1326
  # <!--
1359
1327
  # rdoc-file=io.c
1360
- # - io.internal_encoding -> encoding
1328
+ # - internal_encoding -> encoding or nil
1361
1329
  # -->
1362
- # Returns the Encoding of the internal string if conversion is specified.
1363
- # Otherwise returns `nil`.
1330
+ # Returns the Encoding object that represents the encoding of the internal
1331
+ # string, if conversion is specified, or `nil` otherwise.
1332
+ #
1333
+ # See [Encodings](rdoc-ref:File@Encodings).
1364
1334
  #
1365
1335
  def internal_encoding: () -> Encoding
1366
1336
 
1367
1337
  # <!--
1368
1338
  # rdoc-file=io.c
1369
- # - ios.ioctl(integer_cmd, arg) -> integer
1339
+ # - ioctl(integer_cmd, argument) -> integer
1370
1340
  # -->
1371
- # Provides a mechanism for issuing low-level commands to control or query I/O
1372
- # devices. Arguments and results are platform dependent. If *arg* is a number,
1373
- # its value is passed directly. If it is a string, it is interpreted as a binary
1374
- # sequence of bytes. On Unix platforms, see `ioctl(2)` for details. Not
1375
- # implemented on all platforms.
1341
+ # Invokes Posix system call [ioctl(2)](https://linux.die.net/man/2/ioctl), which
1342
+ # issues a low-level command to an I/O device.
1343
+ #
1344
+ # Issues a low-level command to an I/O device. The arguments and returned value
1345
+ # are platform-dependent. The effect of the call is platform-dependent.
1346
+ #
1347
+ # If argument `argument` is an integer, it is passed directly; if it is a
1348
+ # string, it is interpreted as a binary sequence of bytes.
1349
+ #
1350
+ # Not implemented on all platforms.
1376
1351
  #
1377
1352
  def ioctl: (Integer integer_cmd, String | Integer arg) -> Integer
1378
1353
 
1379
1354
  # <!--
1380
1355
  # rdoc-file=io.c
1381
- # - ios.isatty -> true or false
1382
- # - ios.tty? -> true or false
1356
+ # - isatty -> true or false
1383
1357
  # -->
1384
- # Returns `true` if *ios* is associated with a terminal device (tty), `false`
1385
- # otherwise.
1358
+ # Returns `true` if the stream is associated with a terminal device (tty),
1359
+ # `false` otherwise:
1360
+ #
1361
+ # f = File.new('t.txt').isatty #=> false
1362
+ # f.close
1363
+ # f = File.new('/dev/tty').isatty #=> true
1364
+ # f.close
1386
1365
  #
1387
- # File.new("testfile").isatty #=> false
1388
- # File.new("/dev/tty").isatty #=> true
1366
+ # IO#tty? is an alias for IO#isatty.
1389
1367
  #
1390
1368
  def isatty: () -> bool
1391
1369
 
1392
1370
  # <!--
1393
1371
  # rdoc-file=io.c
1394
- # - ios.lineno -> integer
1372
+ # - lineno -> integer
1395
1373
  # -->
1396
- # Returns the current line number in *ios*. The stream must be opened for
1397
- # reading. #lineno counts the number of times #gets is called rather than the
1398
- # number of newlines encountered. The two values will differ if #gets is called
1399
- # with a separator other than newline.
1400
- #
1401
- # Methods that use `$/` like #each, #lines and #readline will also increment
1402
- # #lineno.
1403
- #
1404
- # See also the `$.` variable.
1405
- #
1406
- # f = File.new("testfile")
1407
- # f.lineno #=> 0
1408
- # f.gets #=> "This is line one\n"
1409
- # f.lineno #=> 1
1410
- # f.gets #=> "This is line two\n"
1411
- # f.lineno #=> 2
1374
+ # Returns the current line number for the stream; see [Line
1375
+ # Number](rdoc-ref:IO@Line+Number).
1412
1376
  #
1413
1377
  def lineno: () -> Integer
1414
1378
 
1415
1379
  # <!--
1416
1380
  # rdoc-file=io.c
1417
- # - ios.lineno = integer -> integer
1381
+ # - lineno = integer -> integer
1418
1382
  # -->
1419
- # Manually sets the current line number to the given value. `$.` is updated only
1420
- # on the next read.
1421
- #
1422
- # f = File.new("testfile")
1423
- # f.gets #=> "This is line one\n"
1424
- # $. #=> 1
1425
- # f.lineno = 1000
1426
- # f.lineno #=> 1000
1427
- # $. #=> 1 # lineno of last read
1428
- # f.gets #=> "This is line two\n"
1429
- # $. #=> 1001 # lineno of last read
1383
+ # Sets and returns the line number for the stream; see [Line
1384
+ # Number](rdoc-ref:IO@Line+Number).
1430
1385
  #
1431
1386
  def lineno=: (Integer arg0) -> Integer
1432
1387
 
1388
+ # <!--
1389
+ # rdoc-file=io.c
1390
+ # - path -> string or nil
1391
+ # -->
1392
+ # Returns the path associated with the IO, or `nil` if there is no path
1393
+ # associated with the IO. It is not guaranteed that the path exists on the
1394
+ # filesystem.
1395
+ #
1396
+ # $stdin.path # => "<STDIN>"
1397
+ #
1398
+ # File.open("testfile") {|f| f.path} # => "testfile"
1399
+ #
1400
+ def path: () -> String?
1401
+
1433
1402
  # <!--
1434
1403
  # rdoc-file=io.c
1435
1404
  # - pid -> integer or nil
@@ -1454,12 +1423,13 @@ class IO < Object
1454
1423
 
1455
1424
  # <!-- rdoc-file=io.c -->
1456
1425
  # Returns the current position (in bytes) in `self` (see
1457
- # [Position](#class-IO-label-Position)):
1426
+ # [Position](rdoc-ref:IO@Position)):
1458
1427
  #
1459
- # f = File.new('t.txt')
1460
- # f.tell # => 0
1461
- # f.readline # => "This is line one.\n"
1462
- # f.tell # => 19
1428
+ # f = File.open('t.txt')
1429
+ # f.tell # => 0
1430
+ # f.gets # => "First line\n"
1431
+ # f.tell # => 12
1432
+ # f.close
1463
1433
  #
1464
1434
  # Related: IO#pos=, IO#seek.
1465
1435
  #
@@ -1472,12 +1442,13 @@ class IO < Object
1472
1442
  # - pos = new_position -> new_position
1473
1443
  # -->
1474
1444
  # Seeks to the given `new_position` (in bytes); see
1475
- # [Position](#class-IO-label-Position):
1445
+ # [Position](rdoc-ref:IO@Position):
1476
1446
  #
1477
1447
  # f = File.open('t.txt')
1478
1448
  # f.tell # => 0
1479
1449
  # f.pos = 20 # => 20
1480
1450
  # f.tell # => 20
1451
+ # f.close
1481
1452
  #
1482
1453
  # Related: IO#seek, IO#tell.
1483
1454
  #
@@ -1485,48 +1456,86 @@ class IO < Object
1485
1456
 
1486
1457
  # <!--
1487
1458
  # rdoc-file=io.c
1488
- # - ios.print -> nil
1489
- # - ios.print(obj, ...) -> nil
1459
+ # - print(*objects) -> nil
1490
1460
  # -->
1491
- # Writes the given object(s) to *ios*. Returns `nil`.
1461
+ # Writes the given objects to the stream; returns `nil`. Appends the output
1462
+ # record separator `$OUTPUT_RECORD_SEPARATOR` (`$\`), if it is not `nil`. See
1463
+ # [Line IO](rdoc-ref:IO@Line+IO).
1492
1464
  #
1493
- # The stream must be opened for writing. Each given object that isn't a string
1494
- # will be converted by calling its `to_s` method. When called without arguments,
1495
- # prints the contents of `$_`.
1465
+ # With argument `objects` given, for each object:
1496
1466
  #
1497
- # If the output field separator (`$,`) is not `nil`, it is inserted between
1498
- # objects. If the output record separator (`$\`) is not `nil`, it is appended to
1499
- # the output.
1467
+ # * Converts via its method `to_s` if not a string.
1468
+ # * Writes to the stream.
1469
+ # * If not the last object, writes the output field separator
1470
+ # `$OUTPUT_FIELD_SEPARATOR` (`$,`) if it is not `nil`.
1500
1471
  #
1501
- # $stdout.print("This is ", 100, " percent.\n")
1502
1472
  #
1503
- # *produces:*
1473
+ # With default separators:
1504
1474
  #
1505
- # This is 100 percent.
1475
+ # f = File.open('t.tmp', 'w+')
1476
+ # objects = [0, 0.0, Rational(0, 1), Complex(0, 0), :zero, 'zero']
1477
+ # p $OUTPUT_RECORD_SEPARATOR
1478
+ # p $OUTPUT_FIELD_SEPARATOR
1479
+ # f.print(*objects)
1480
+ # f.rewind
1481
+ # p f.read
1482
+ # f.close
1483
+ #
1484
+ # Output:
1485
+ #
1486
+ # nil
1487
+ # nil
1488
+ # "00.00/10+0izerozero"
1489
+ #
1490
+ # With specified separators:
1491
+ #
1492
+ # $\ = "\n"
1493
+ # $, = ','
1494
+ # f.rewind
1495
+ # f.print(*objects)
1496
+ # f.rewind
1497
+ # p f.read
1498
+ #
1499
+ # Output:
1500
+ #
1501
+ # "0,0.0,0/1,0+0i,zero,zero\n"
1502
+ #
1503
+ # With no argument given, writes the content of `$_` (which is usually the most
1504
+ # recent user input):
1505
+ #
1506
+ # f = File.open('t.tmp', 'w+')
1507
+ # gets # Sets $_ to the most recent user input.
1508
+ # f.print
1509
+ # f.close
1506
1510
  #
1507
1511
  def print: (*untyped arg0) -> NilClass
1508
1512
 
1509
1513
  # <!--
1510
1514
  # rdoc-file=io.c
1511
- # - ios.printf(format_string [, obj, ...]) -> nil
1515
+ # - printf(format_string, *objects) -> nil
1512
1516
  # -->
1513
- # Formats and writes to *ios*, converting parameters under control of the format
1514
- # string. See Kernel#sprintf for details.
1517
+ # Formats and writes `objects` to the stream.
1518
+ #
1519
+ # For details on `format_string`, see [Format
1520
+ # Specifications](rdoc-ref:format_specifications.rdoc).
1515
1521
  #
1516
1522
  def printf: (String format_string, *untyped arg0) -> NilClass
1517
1523
 
1518
1524
  # <!--
1519
1525
  # rdoc-file=io.c
1520
- # - ios.putc(obj) -> obj
1526
+ # - putc(object) -> object
1521
1527
  # -->
1522
- # If *obj* is Numeric, write the character whose code is the least-significant
1523
- # byte of *obj*. If *obj* is String, write the first character of *obj* to
1524
- # *ios*. Otherwise, raise TypeError.
1528
+ # Writes a character to the stream. See [Character
1529
+ # IO](rdoc-ref:IO@Character+IO).
1530
+ #
1531
+ # If `object` is numeric, converts to integer if necessary, then writes the
1532
+ # character whose code is the least significant byte; if `object` is a string,
1533
+ # writes the first character:
1525
1534
  #
1526
1535
  # $stdout.putc "A"
1527
1536
  # $stdout.putc 65
1528
1537
  #
1529
- # *produces:*
1538
+ # Output:
1530
1539
  #
1531
1540
  # AA
1532
1541
  #
@@ -1534,40 +1543,60 @@ class IO < Object
1534
1543
 
1535
1544
  # <!--
1536
1545
  # rdoc-file=io.c
1537
- # - ios.puts(obj, ...) -> nil
1546
+ # - puts(*objects) -> nil
1538
1547
  # -->
1539
- # Writes the given object(s) to *ios*. Writes a newline after any that do not
1540
- # already end with a newline sequence. Returns `nil`.
1548
+ # Writes the given `objects` to the stream, which must be open for writing;
1549
+ # returns `nil`.\ Writes a newline after each that does not already end with a
1550
+ # newline sequence. If called without arguments, writes a newline. See [Line
1551
+ # IO](rdoc-ref:IO@Line+IO).
1541
1552
  #
1542
- # The stream must be opened for writing. If called with an array argument,
1543
- # writes each element on a new line. Each given object that isn't a string or
1544
- # array will be converted by calling its `to_s` method. If called without
1545
- # arguments, outputs a single newline.
1553
+ # Note that each added newline is the character `"\n"<//tt>, not the output
1554
+ # record separator (<tt>$\`).
1546
1555
  #
1547
- # $stdout.puts("this", "is", ["a", "test"])
1556
+ # Treatment for each object:
1548
1557
  #
1549
- # *produces:*
1558
+ # * String: writes the string.
1559
+ # * Neither string nor array: writes `object.to_s`.
1560
+ # * Array: writes each element of the array; arrays may be nested.
1561
+ #
1562
+ #
1563
+ # To keep these examples brief, we define this helper method:
1564
+ #
1565
+ # def show(*objects)
1566
+ # # Puts objects to file.
1567
+ # f = File.new('t.tmp', 'w+')
1568
+ # f.puts(objects)
1569
+ # # Return file content.
1570
+ # f.rewind
1571
+ # p f.read
1572
+ # f.close
1573
+ # end
1550
1574
  #
1551
- # this
1552
- # is
1553
- # a
1554
- # test
1575
+ # # Strings without newlines.
1576
+ # show('foo', 'bar', 'baz') # => "foo\nbar\nbaz\n"
1577
+ # # Strings, some with newlines.
1578
+ # show("foo\n", 'bar', "baz\n") # => "foo\nbar\nbaz\n"
1555
1579
  #
1556
- # Note that `puts` always uses newlines and is not affected by the output record
1557
- # separator (`$\`).
1580
+ # # Neither strings nor arrays:
1581
+ # show(0, 0.0, Rational(0, 1), Complex(9, 0), :zero)
1582
+ # # => "0\n0.0\n0/1\n9+0i\nzero\n"
1583
+ #
1584
+ # # Array of strings.
1585
+ # show(['foo', "bar\n", 'baz']) # => "foo\nbar\nbaz\n"
1586
+ # # Nested arrays.
1587
+ # show([[[0, 1], 2, 3], 4, 5]) # => "0\n1\n2\n3\n4\n5\n"
1558
1588
  #
1559
1589
  def puts: (*untyped arg0) -> NilClass
1560
1590
 
1561
1591
  # <!--
1562
1592
  # rdoc-file=io.c
1563
- # - read(maxlen = nil) -> string or nil
1564
- # - read(maxlen = nil, out_string) -> out_string or nil
1593
+ # - read(maxlen = nil, out_string = nil) -> new_string, out_string, or nil
1565
1594
  # -->
1566
- # Reads bytes from the stream (in binary mode):
1595
+ # Reads bytes from the stream; the stream must be opened for reading (see
1596
+ # [Access Modes](rdoc-ref:File@Access+Modes)):
1567
1597
  #
1568
- # * If `maxlen` is `nil`, reads all bytes.
1569
- # * Otherwise reads `maxlen` bytes, if available.
1570
- # * Otherwise reads all bytes.
1598
+ # * If `maxlen` is `nil`, reads all bytes using the stream's data mode.
1599
+ # * Otherwise reads up to `maxlen` bytes in binary mode.
1571
1600
  #
1572
1601
  #
1573
1602
  # Returns a string (either a new string or the given `out_string`) containing
@@ -1589,11 +1618,12 @@ class IO < Object
1589
1618
  #
1590
1619
  # f = File.new('t.txt')
1591
1620
  # f.read
1592
- # # => "This is line one.\nThis is the second line.\nThis is the third line.\n"
1621
+ # # => "First line\nSecond line\n\nFourth line\nFifth line\n"
1593
1622
  # f.rewind
1594
- # f.read(40) # => "This is line one.\r\nThis is the second li"
1595
- # f.read(40) # => "ne.\r\nThis is the third line.\r\n"
1596
- # f.read(40) # => nil
1623
+ # f.read(30) # => "First line\r\nSecond line\r\n\r\nFou"
1624
+ # f.read(30) # => "rth line\r\nFifth line\r\n"
1625
+ # f.read(30) # => nil
1626
+ # f.close
1597
1627
  #
1598
1628
  # If `maxlen` is zero, returns an empty string.
1599
1629
  #
@@ -1604,18 +1634,19 @@ class IO < Object
1604
1634
  #
1605
1635
  # f = File.new('t.txt')
1606
1636
  # s = 'foo' # => "foo"
1607
- # f.read(nil, s) # => "This is line one.\nThis is the second line.\nThis is the third line.\n"
1608
- # s # => "This is line one.\nThis is the second line.\nThis is the third line.\n"
1637
+ # f.read(nil, s) # => "First line\nSecond line\n\nFourth line\nFifth line\n"
1638
+ # s # => "First line\nSecond line\n\nFourth line\nFifth line\n"
1609
1639
  # f.rewind
1610
1640
  # s = 'bar'
1611
- # f.read(40, s) # => "This is line one.\r\nThis is the second li"
1612
- # s # => "This is line one.\r\nThis is the second li"
1641
+ # f.read(30, s) # => "First line\r\nSecond line\r\n\r\nFou"
1642
+ # s # => "First line\r\nSecond line\r\n\r\nFou"
1613
1643
  # s = 'baz'
1614
- # f.read(40, s) # => "ne.\r\nThis is the third line.\r\n"
1615
- # s # => "ne.\r\nThis is the third line.\r\n"
1644
+ # f.read(30, s) # => "rth line\r\nFifth line\r\n"
1645
+ # s # => "rth line\r\nFifth line\r\n"
1616
1646
  # s = 'bat'
1617
- # f.read(40, s) # => nil
1647
+ # f.read(30, s) # => nil
1618
1648
  # s # => ""
1649
+ # f.close
1619
1650
  #
1620
1651
  # Note that this method behaves like the fread() function in C. This means it
1621
1652
  # retries to invoke read(2) system calls to read data with the specified maxlen
@@ -1627,6 +1658,8 @@ class IO < Object
1627
1658
  # If you need the behavior like a single read(2) system call, consider
1628
1659
  # #readpartial, #read_nonblock, and #sysread.
1629
1660
  #
1661
+ # Related: IO#write.
1662
+ #
1630
1663
  def read: (?int? length, ?string outbuf) -> String?
1631
1664
 
1632
1665
  # <!--
@@ -1687,54 +1720,113 @@ class IO < Object
1687
1720
 
1688
1721
  # <!--
1689
1722
  # rdoc-file=io.c
1690
- # - ios.readbyte -> integer
1723
+ # - readbyte -> integer
1691
1724
  # -->
1692
- # Reads a byte as with IO#getbyte, but raises an EOFError on end of file.
1725
+ # Reads and returns the next byte (in range 0..255) from the stream; raises
1726
+ # EOFError if already at end-of-stream. See [Byte IO](rdoc-ref:IO@Byte+IO).
1727
+ #
1728
+ # f = File.open('t.txt')
1729
+ # f.readbyte # => 70
1730
+ # f.close
1731
+ # f = File.open('t.rus')
1732
+ # f.readbyte # => 209
1733
+ # f.close
1734
+ #
1735
+ # Related: IO#getbyte (will not raise EOFError).
1693
1736
  #
1694
1737
  def readbyte: () -> Integer
1695
1738
 
1696
1739
  # <!--
1697
1740
  # rdoc-file=io.c
1698
- # - ios.readchar -> string
1741
+ # - readchar -> string
1699
1742
  # -->
1700
- # Reads a one-character string from *ios*. Raises an EOFError on end of file.
1743
+ # Reads and returns the next 1-character string from the stream; raises EOFError
1744
+ # if already at end-of-stream. See [Character IO](rdoc-ref:IO@Character+IO).
1701
1745
  #
1702
- # f = File.new("testfile")
1703
- # f.readchar #=> "h"
1704
- # f.readchar #=> "e"
1746
+ # f = File.open('t.txt')
1747
+ # f.readchar # => "F"
1748
+ # f.close
1749
+ # f = File.open('t.rus')
1750
+ # f.readchar.ord # => 1090
1751
+ # f.close
1752
+ #
1753
+ # Related: IO#getc (will not raise EOFError).
1705
1754
  #
1706
1755
  def readchar: () -> String
1707
1756
 
1708
1757
  # <!--
1709
1758
  # rdoc-file=io.c
1710
- # - ios.readline(sep=$/ [, getline_args]) -> string
1711
- # - ios.readline(limit [, getline_args]) -> string
1712
- # - ios.readline(sep, limit [, getline_args]) -> string
1759
+ # - readline(sep = $/, chomp: false) -> string
1760
+ # - readline(limit, chomp: false) -> string
1761
+ # - readline(sep, limit, chomp: false) -> string
1762
+ # -->
1763
+ # Reads a line as with IO#gets, but raises EOFError if already at end-of-stream.
1764
+ #
1765
+ # Optional keyword argument `chomp` specifies whether line separators are to be
1766
+ # omitted.
1767
+ #
1768
+ def readline: (?String sep, ?Integer limit) -> String
1769
+
1770
+ # <!--
1771
+ # rdoc-file=io.c
1772
+ # - readlines(sep = $/, chomp: false) -> array
1773
+ # - readlines(limit, chomp: false) -> array
1774
+ # - readlines(sep, limit, chomp: false) -> array
1713
1775
  # -->
1714
- # Reads a line as with IO#gets, but raises an EOFError on end of file.
1776
+ # Reads and returns all remaining line from the stream; does not modify `$_`.
1777
+ # See [Line IO](rdoc-ref:IO@Line+IO).
1778
+ #
1779
+ # With no arguments given, returns lines as determined by line separator `$/`,
1780
+ # or `nil` if none:
1781
+ #
1782
+ # f = File.new('t.txt')
1783
+ # f.readlines
1784
+ # # => ["First line\n", "Second line\n", "\n", "Fourth line\n", "Fifth line\n"]
1785
+ # f.readlines # => []
1786
+ # f.close
1787
+ #
1788
+ # With only string argument `sep` given, returns lines as determined by line
1789
+ # separator `sep`, or `nil` if none; see [Line
1790
+ # Separator](rdoc-ref:IO@Line+Separator):
1791
+ #
1792
+ # f = File.new('t.txt')
1793
+ # f.readlines('li')
1794
+ # # => ["First li", "ne\nSecond li", "ne\n\nFourth li", "ne\nFifth li", "ne\n"]
1795
+ # f.close
1796
+ #
1797
+ # The two special values for `sep` are honored:
1798
+ #
1799
+ # f = File.new('t.txt')
1800
+ # # Get all into one string.
1801
+ # f.readlines(nil)
1802
+ # # => ["First line\nSecond line\n\nFourth line\nFifth line\n"]
1803
+ # # Get paragraphs (up to two line separators).
1804
+ # f.rewind
1805
+ # f.readlines('')
1806
+ # # => ["First line\nSecond line\n\n", "Fourth line\nFifth line\n"]
1807
+ # f.close
1808
+ #
1809
+ # With only integer argument `limit` given, limits the number of bytes in each
1810
+ # line; see [Line Limit](rdoc-ref:IO@Line+Limit):
1715
1811
  #
1716
- def readline: (?String sep, ?Integer limit) -> String
1717
-
1718
- # <!--
1719
- # rdoc-file=io.c
1720
- # - ios.readlines(sep=$/ [, getline_args]) -> array
1721
- # - ios.readlines(limit [, getline_args]) -> array
1722
- # - ios.readlines(sep, limit [, getline_args]) -> array
1723
- # -->
1724
- # Reads all of the lines in *ios*, and returns them in an array. Lines are
1725
- # separated by the optional *sep*. If *sep* is `nil`, the rest of the stream is
1726
- # returned as a single record. If the first argument is an integer, or an
1727
- # optional second argument is given, the returning string would not be longer
1728
- # than the given value in bytes. The stream must be opened for reading or an
1729
- # IOError will be raised.
1812
+ # f = File.new('t.txt')
1813
+ # f.readlines(8)
1814
+ # # => ["First li", "ne\n", "Second l", "ine\n", "\n", "Fourth l", "ine\n", "Fifth li", "ne\n"]
1815
+ # f.close
1730
1816
  #
1731
- # f = File.new("testfile")
1732
- # f.readlines[0] #=> "This is line one\n"
1817
+ # With arguments `sep` and `limit` given, combines the two behaviors:
1733
1818
  #
1734
- # f = File.new("testfile", chomp: true)
1735
- # f.readlines[0] #=> "This is line one"
1819
+ # * Returns lines as determined by line separator `sep`.
1820
+ # * But returns no more bytes in a line than are allowed by the limit.
1736
1821
  #
1737
- # See IO.readlines for details about getline_args.
1822
+ #
1823
+ # Optional keyword argument `chomp` specifies whether line separators are to be
1824
+ # omitted:
1825
+ #
1826
+ # f = File.new('t.txt')
1827
+ # f.readlines(chomp: true)
1828
+ # # => ["First line", "Second line", "", "Fourth line", "Fifth line"]
1829
+ # f.close
1738
1830
  #
1739
1831
  def readlines: (?String sep, ?Integer limit) -> ::Array[String]
1740
1832
 
@@ -1758,20 +1850,21 @@ class IO < Object
1758
1850
  # string:
1759
1851
  #
1760
1852
  # f = File.new('t.txt')
1761
- # f.readpartial(30) # => "This is line one.\nThis is the"
1762
- # f.readpartial(30) # => " second line.\nThis is the thi"
1763
- # f.readpartial(30) # => "rd line.\n"
1764
- # f.eof # => true
1765
- # f.readpartial(30) # Raises EOFError.
1853
+ # f.readpartial(20) # => "First line\nSecond l"
1854
+ # f.readpartial(20) # => "ine\n\nFourth line\n"
1855
+ # f.readpartial(20) # => "Fifth line\n"
1856
+ # f.readpartial(20) # Raises EOFError.
1857
+ # f.close
1766
1858
  #
1767
1859
  # With both argument `maxlen` and string argument `out_string` given, returns
1768
1860
  # modified `out_string`:
1769
1861
  #
1770
1862
  # f = File.new('t.txt')
1771
1863
  # s = 'foo'
1772
- # f.readpartial(30, s) # => "This is line one.\nThis is the"
1864
+ # f.readpartial(20, s) # => "First line\nSecond l"
1773
1865
  # s = 'bar'
1774
1866
  # f.readpartial(0, s) # => ""
1867
+ # f.close
1775
1868
  #
1776
1869
  # This method is useful for a stream such as a pipe, a socket, or a tty. It
1777
1870
  # blocks only when no data is immediately available. This means that it blocks
@@ -1835,18 +1928,34 @@ class IO < Object
1835
1928
 
1836
1929
  # <!--
1837
1930
  # rdoc-file=io.c
1838
- # - ios.reopen(other_IO) -> ios
1839
- # - ios.reopen(path, mode [, opt]) -> ios
1931
+ # - reopen(other_io) -> self
1932
+ # - reopen(path, mode = 'r', **opts) -> self
1840
1933
  # -->
1841
- # Reassociates *ios* with the I/O stream given in *other_IO* or to a new stream
1842
- # opened on *path*. This may dynamically change the actual class of this stream.
1843
- # The `mode` and `opt` parameters accept the same values as IO.open.
1934
+ # Reassociates the stream with another stream, which may be of a different
1935
+ # class. This method may be used to redirect an existing stream to a new
1936
+ # destination.
1937
+ #
1938
+ # With argument `other_io` given, reassociates with that stream:
1939
+ #
1940
+ # # Redirect $stdin from a file.
1941
+ # f = File.open('t.txt')
1942
+ # $stdin.reopen(f)
1943
+ # f.close
1944
+ #
1945
+ # # Redirect $stdout to a file.
1946
+ # f = File.open('t.tmp', 'w')
1947
+ # $stdout.reopen(f)
1948
+ # f.close
1949
+ #
1950
+ # With argument `path` given, reassociates with a new stream to that file path:
1951
+ #
1952
+ # $stdin.reopen('t.txt')
1953
+ # $stdout.reopen('t.tmp', 'w')
1954
+ #
1955
+ # Optional keyword arguments `opts` specify:
1844
1956
  #
1845
- # f1 = File.new("testfile")
1846
- # f2 = File.new("testfile")
1847
- # f2.readlines[0] #=> "This is line one\n"
1848
- # f2.reopen(f1) #=> #<File:testfile>
1849
- # f2.readlines[0] #=> "This is line one\n"
1957
+ # * [Open Options](rdoc-ref:IO@Open+Options).
1958
+ # * [Encoding options](rdoc-ref:encodings.rdoc@Encoding+Options).
1850
1959
  #
1851
1960
  def reopen: (IO other_IO_or_path) -> IO
1852
1961
  | (String other_IO_or_path, ?String mode_str) -> IO
@@ -1856,18 +1965,19 @@ class IO < Object
1856
1965
  # - rewind -> 0
1857
1966
  # -->
1858
1967
  # Repositions the stream to its beginning, setting both the position and the
1859
- # line number to zero; see [Position](#class-IO-label-Position) and [Line
1860
- # Number](#class-IO-label-Line+Number):
1968
+ # line number to zero; see [Position](rdoc-ref:IO@Position) and [Line
1969
+ # Number](rdoc-ref:IO@Line+Number):
1861
1970
  #
1862
1971
  # f = File.open('t.txt')
1863
1972
  # f.tell # => 0
1864
1973
  # f.lineno # => 0
1865
- # f.readline # => "This is line one.\n"
1866
- # f.tell # => 19
1974
+ # f.gets # => "First line\n"
1975
+ # f.tell # => 12
1867
1976
  # f.lineno # => 1
1868
1977
  # f.rewind # => 0
1869
1978
  # f.tell # => 0
1870
1979
  # f.lineno # => 0
1980
+ # f.close
1871
1981
  #
1872
1982
  # Note that this method cannot be used with streams such as pipes, ttys, and
1873
1983
  # sockets.
@@ -1879,7 +1989,7 @@ class IO < Object
1879
1989
  # - seek(offset, whence = IO::SEEK_SET) -> 0
1880
1990
  # -->
1881
1991
  # Seeks to the position given by integer `offset` (see
1882
- # [Position](#class-IO-label-Position)) and constant `whence`, which is one of:
1992
+ # [Position](rdoc-ref:IO@Position)) and constant `whence`, which is one of:
1883
1993
  #
1884
1994
  # * `:CUR` or `IO::SEEK_CUR`: Repositions the stream to its current position
1885
1995
  # plus the given `offset`:
@@ -1890,6 +2000,7 @@ class IO < Object
1890
2000
  # f.tell # => 20
1891
2001
  # f.seek(-10, :CUR) # => 0
1892
2002
  # f.tell # => 10
2003
+ # f.close
1893
2004
  #
1894
2005
  # * `:END` or `IO::SEEK_END`: Repositions the stream to its end plus the given
1895
2006
  # `offset`:
@@ -1897,11 +2008,12 @@ class IO < Object
1897
2008
  # f = File.open('t.txt')
1898
2009
  # f.tell # => 0
1899
2010
  # f.seek(0, :END) # => 0 # Repositions to stream end.
1900
- # f.tell # => 70
2011
+ # f.tell # => 52
1901
2012
  # f.seek(-20, :END) # => 0
1902
- # f.tell # => 50
2013
+ # f.tell # => 32
1903
2014
  # f.seek(-40, :END) # => 0
1904
- # f.tell # => 30
2015
+ # f.tell # => 12
2016
+ # f.close
1905
2017
  #
1906
2018
  # * `:SET` or `IO:SEEK_SET`: Repositions the stream to the given `offset`:
1907
2019
  #
@@ -1911,6 +2023,7 @@ class IO < Object
1911
2023
  # f.tell # => 20
1912
2024
  # f.seek(40, :SET) # => 0
1913
2025
  # f.tell # => 40
2026
+ # f.close
1914
2027
  #
1915
2028
  #
1916
2029
  # Related: IO#pos=, IO#tell.
@@ -1919,39 +2032,49 @@ class IO < Object
1919
2032
 
1920
2033
  # <!--
1921
2034
  # rdoc-file=io.c
1922
- # - io.set_encoding(ext_enc) -> io
1923
- # - io.set_encoding("ext_enc:int_enc") -> io
1924
- # - io.set_encoding(ext_enc, int_enc) -> io
1925
- # - io.set_encoding("ext_enc:int_enc", opt) -> io
1926
- # - io.set_encoding(ext_enc, int_enc, opt) -> io
2035
+ # - set_encoding(ext_enc) -> self
2036
+ # - set_encoding(ext_enc, int_enc, **enc_opts) -> self
2037
+ # - set_encoding('ext_enc:int_enc', **enc_opts) -> self
1927
2038
  # -->
1928
- # If single argument is specified, read string from io is tagged with the
1929
- # encoding specified. If encoding is a colon separated two encoding names
1930
- # "A:B", the read string is converted from encoding A (external encoding) to
1931
- # encoding B (internal encoding), then tagged with B. If two arguments are
1932
- # specified, those must be encoding objects or encoding names, and the first one
1933
- # is the external encoding, and the second one is the internal encoding. If the
1934
- # external encoding and the internal encoding is specified, optional hash
1935
- # argument specify the conversion option.
2039
+ # See [Encodings](rdoc-ref:File@Encodings).
2040
+ #
2041
+ # Argument `ext_enc`, if given, must be an Encoding object; it is assigned as
2042
+ # the encoding for the stream.
2043
+ #
2044
+ # Argument `int_enc`, if given, must be an Encoding object; it is assigned as
2045
+ # the encoding for the internal string.
2046
+ #
2047
+ # Argument `'ext_enc:int_enc'`, if given, is a string containing two
2048
+ # colon-separated encoding names; corresponding Encoding objects are assigned as
2049
+ # the external and internal encodings for the stream.
2050
+ #
2051
+ # Optional keyword arguments `enc_opts` specify [Encoding
2052
+ # options](rdoc-ref:encodings.rdoc@Encoding+Options).
1936
2053
  #
1937
2054
  def set_encoding: (?String | Encoding ext_or_ext_int_enc) -> self
1938
2055
  | (?String | Encoding ext_or_ext_int_enc, ?String | Encoding int_enc) -> self
1939
2056
 
1940
2057
  # <!--
1941
2058
  # rdoc-file=io.c
1942
- # - ios.set_encoding_by_bom -> encoding or nil
2059
+ # - set_encoding_by_bom -> encoding or nil
1943
2060
  # -->
1944
- # Checks if `ios` starts with a BOM, and then consumes it and sets the external
1945
- # encoding. Returns the result encoding if found, or nil. If `ios` is not
1946
- # binmode or its encoding has been set already, an exception will be raised.
2061
+ # If the stream begins with a BOM ([byte order
2062
+ # marker](https://en.wikipedia.org/wiki/Byte_order_mark)), consumes the BOM and
2063
+ # sets the external encoding accordingly; returns the result encoding if found,
2064
+ # or `nil` otherwise:
2065
+ #
2066
+ # File.write('t.tmp', "\u{FEFF}abc")
2067
+ # io = File.open('t.tmp', 'rb')
2068
+ # io.set_encoding_by_bom # => #<Encoding:UTF-8>
2069
+ # io.close
1947
2070
  #
1948
- # File.write("bom.txt", "\u{FEFF}abc")
1949
- # ios = File.open("bom.txt", "rb")
1950
- # ios.set_encoding_by_bom #=> #<Encoding:UTF-8>
2071
+ # File.write('t.tmp', 'abc')
2072
+ # io = File.open('t.tmp', 'rb')
2073
+ # io.set_encoding_by_bom # => nil
2074
+ # io.close
1951
2075
  #
1952
- # File.write("nobom.txt", "abc")
1953
- # ios = File.open("nobom.txt", "rb")
1954
- # ios.set_encoding_by_bom #=> nil
2076
+ # Raises an exception if the stream is not binmode or its encoding has already
2077
+ # been set.
1955
2078
  #
1956
2079
  def set_encoding_by_bom: () -> Encoding?
1957
2080
 
@@ -1981,6 +2104,7 @@ class IO < Object
1981
2104
  # f.sync # => false
1982
2105
  # f.sync = true
1983
2106
  # f.sync # => true
2107
+ # f.close
1984
2108
  #
1985
2109
  def sync: () -> bool
1986
2110
 
@@ -2004,6 +2128,7 @@ class IO < Object
2004
2128
  # f.sync # => false
2005
2129
  # f.sync = true
2006
2130
  # f.sync # => true
2131
+ # f.close
2007
2132
  #
2008
2133
  # Related: IO#fsync.
2009
2134
  #
@@ -2011,46 +2136,41 @@ class IO < Object
2011
2136
 
2012
2137
  # <!--
2013
2138
  # rdoc-file=io.c
2014
- # - ios.sysread(maxlen[, outbuf]) -> string
2139
+ # - sysread(maxlen) -> string
2140
+ # - sysread(maxlen, out_string) -> string
2015
2141
  # -->
2016
- # Reads *maxlen* bytes from *ios* using a low-level read and returns them as a
2017
- # string. Do not mix with other methods that read from *ios* or you may get
2018
- # unpredictable results.
2142
+ # Behaves like IO#readpartial, except that it uses low-level system functions.
2019
2143
  #
2020
- # If the optional *outbuf* argument is present, it must reference a String,
2021
- # which will receive the data. The *outbuf* will contain only the received data
2022
- # after the method call even if it is not empty at the beginning.
2023
- #
2024
- # Raises SystemCallError on error and EOFError at end of file.
2025
- #
2026
- # f = File.new("testfile")
2027
- # f.sysread(16) #=> "This is line one"
2144
+ # This method should not be used with other stream-reader methods.
2028
2145
  #
2029
2146
  def sysread: (Integer maxlen, String outbuf) -> String
2030
2147
 
2031
2148
  # <!--
2032
2149
  # rdoc-file=io.c
2033
- # - ios.sysseek(offset, whence=IO::SEEK_SET) -> integer
2150
+ # - sysseek(offset, whence = IO::SEEK_SET) -> integer
2034
2151
  # -->
2035
- # Seeks to a given *offset* in the stream according to the value of *whence*
2036
- # (see IO#seek for values of *whence*). Returns the new offset into the file.
2152
+ # Behaves like IO#seek, except that it:
2037
2153
  #
2038
- # f = File.new("testfile")
2039
- # f.sysseek(-13, IO::SEEK_END) #=> 53
2040
- # f.sysread(10) #=> "And so on."
2154
+ # * Uses low-level system functions.
2155
+ # * Returns the new position.
2041
2156
  #
2042
2157
  def sysseek: (Integer amount, ?Integer whence) -> Integer
2043
2158
 
2044
2159
  # <!--
2045
2160
  # rdoc-file=io.c
2046
- # - ios.syswrite(string) -> integer
2161
+ # - syswrite(object) -> integer
2047
2162
  # -->
2048
- # Writes the given string to *ios* using a low-level write. Returns the number
2049
- # of bytes written. Do not mix with other methods that write to *ios* or you may
2050
- # get unpredictable results. Raises SystemCallError on error.
2163
+ # Writes the given `object` to self, which must be opened for writing (see
2164
+ # Modes); returns the number bytes written. If `object` is not a string is
2165
+ # converted via method to_s:
2166
+ #
2167
+ # f = File.new('t.tmp', 'w')
2168
+ # f.syswrite('foo') # => 3
2169
+ # f.syswrite(30) # => 2
2170
+ # f.syswrite(:foo) # => 3
2171
+ # f.close
2051
2172
  #
2052
- # f = File.new("out", "w")
2053
- # f.syswrite("ABCDEF") #=> 6
2173
+ # This methods should not be used with other stream-writer methods.
2054
2174
  #
2055
2175
  def syswrite: (_ToS arg0) -> Integer
2056
2176
 
@@ -2059,12 +2179,13 @@ class IO < Object
2059
2179
  # - tell -> integer
2060
2180
  # -->
2061
2181
  # Returns the current position (in bytes) in `self` (see
2062
- # [Position](#class-IO-label-Position)):
2182
+ # [Position](rdoc-ref:IO@Position)):
2063
2183
  #
2064
- # f = File.new('t.txt')
2065
- # f.tell # => 0
2066
- # f.readline # => "This is line one.\n"
2067
- # f.tell # => 19
2184
+ # f = File.open('t.txt')
2185
+ # f.tell # => 0
2186
+ # f.gets # => "First line\n"
2187
+ # f.tell # => 12
2188
+ # f.close
2068
2189
  #
2069
2190
  # Related: IO#pos=, IO#seek.
2070
2191
  #
@@ -2072,6 +2193,34 @@ class IO < Object
2072
2193
  #
2073
2194
  def tell: () -> Integer
2074
2195
 
2196
+ # <!--
2197
+ # rdoc-file=io.c
2198
+ # - timeout -> duration or nil
2199
+ # -->
2200
+ # Get the internal timeout duration or nil if it was not set.
2201
+ #
2202
+ def timeout: () -> Numeric?
2203
+
2204
+ # <!--
2205
+ # rdoc-file=io.c
2206
+ # - timeout = duration -> duration
2207
+ # - timeout = nil -> nil
2208
+ # -->
2209
+ # Set the internal timeout to the specified duration or nil. The timeout applies
2210
+ # to all blocking operations where possible.
2211
+ #
2212
+ # This affects the following methods (but is not limited to): #gets, #puts,
2213
+ # #read, #write, #wait_readable and #wait_writable. This also affects blocking
2214
+ # socket operations like Socket#accept and Socket#connect.
2215
+ #
2216
+ # Some operations like File#open and IO#close are not affected by the timeout. A
2217
+ # timeout during a write operation may leave the IO in an inconsistent state,
2218
+ # e.g. data was partially written. Generally speaking, a timeout is a last ditch
2219
+ # effort to prevent an application from hanging on slow I/O operations, such as
2220
+ # those that occur during a slowloris attack.
2221
+ #
2222
+ def timeout=: (Numeric?) -> void
2223
+
2075
2224
  # <!--
2076
2225
  # rdoc-file=io.c
2077
2226
  # - to_io -> self
@@ -2081,78 +2230,98 @@ class IO < Object
2081
2230
  def to_io: () -> self
2082
2231
 
2083
2232
  # <!-- rdoc-file=io.c -->
2084
- # Returns `true` if *ios* is associated with a terminal device (tty), `false`
2085
- # otherwise.
2233
+ # Returns `true` if the stream is associated with a terminal device (tty),
2234
+ # `false` otherwise:
2235
+ #
2236
+ # f = File.new('t.txt').isatty #=> false
2237
+ # f.close
2238
+ # f = File.new('/dev/tty').isatty #=> true
2239
+ # f.close
2086
2240
  #
2087
- # File.new("testfile").isatty #=> false
2088
- # File.new("/dev/tty").isatty #=> true
2241
+ # IO#tty? is an alias for IO#isatty.
2089
2242
  #
2090
2243
  def tty?: () -> bool
2091
2244
 
2092
2245
  # <!--
2093
2246
  # rdoc-file=io.c
2094
- # - ios.ungetbyte(string) -> nil
2095
- # - ios.ungetbyte(integer) -> nil
2247
+ # - ungetbyte(integer) -> nil
2248
+ # - ungetbyte(string) -> nil
2096
2249
  # -->
2097
- # Pushes back bytes (passed as a parameter) onto *ios*, such that a subsequent
2098
- # buffered read will return it. It is only guaranteed to support a single byte,
2099
- # and only if ungetbyte or ungetc has not already been called on *ios* since the
2100
- # previous read of at least a single byte from *ios*. However, it can support
2101
- # additional bytes if there is space in the internal buffer to allow for it.
2250
+ # Pushes back ("unshifts") the given data onto the stream's buffer, placing the
2251
+ # data so that it is next to be read; returns `nil`. See [Byte
2252
+ # IO](rdoc-ref:IO@Byte+IO).
2102
2253
  #
2103
- # f = File.new("testfile") #=> #<File:testfile>
2104
- # b = f.getbyte #=> 0x38
2105
- # f.ungetbyte(b) #=> nil
2106
- # f.getbyte #=> 0x38
2254
+ # Note that:
2107
2255
  #
2108
- # If given an integer, only uses the lower 8 bits of the integer as the byte to
2109
- # push.
2256
+ # * Calling the method has no effect with unbuffered reads (such as
2257
+ # IO#sysread).
2258
+ # * Calling #rewind on the stream discards the pushed-back data.
2110
2259
  #
2111
- # f = File.new("testfile") #=> #<File:testfile>
2112
- # f.ungetbyte(0x102) #=> nil
2113
- # f.getbyte #=> 0x2
2114
2260
  #
2115
- # Calling this method prepends to the existing buffer, even if the method has
2116
- # already been called previously:
2261
+ # When argument `integer` is given, uses only its low-order byte:
2117
2262
  #
2118
- # f = File.new("testfile") #=> #<File:testfile>
2119
- # f.ungetbyte("ab") #=> nil
2120
- # f.ungetbyte("cd") #=> nil
2121
- # f.read(5) #=> "cdab8"
2263
+ # File.write('t.tmp', '012')
2264
+ # f = File.open('t.tmp')
2265
+ # f.ungetbyte(0x41) # => nil
2266
+ # f.read # => "A012"
2267
+ # f.rewind
2268
+ # f.ungetbyte(0x4243) # => nil
2269
+ # f.read # => "C012"
2270
+ # f.close
2122
2271
  #
2123
- # Has no effect with unbuffered reads (such as IO#sysread).
2272
+ # When argument `string` is given, uses all bytes:
2273
+ #
2274
+ # File.write('t.tmp', '012')
2275
+ # f = File.open('t.tmp')
2276
+ # f.ungetbyte('A') # => nil
2277
+ # f.read # => "A012"
2278
+ # f.rewind
2279
+ # f.ungetbyte('BCDE') # => nil
2280
+ # f.read # => "BCDE012"
2281
+ # f.close
2124
2282
  #
2125
2283
  def ungetbyte: (String | Integer arg0) -> NilClass
2126
2284
 
2127
2285
  # <!--
2128
2286
  # rdoc-file=io.c
2129
- # - ios.ungetc(integer) -> nil
2130
- # - ios.ungetc(string) -> nil
2287
+ # - ungetc(integer) -> nil
2288
+ # - ungetc(string) -> nil
2131
2289
  # -->
2132
- # Pushes back characters (passed as a parameter) onto *ios*, such that a
2133
- # subsequent buffered read will return it. It is only guaranteed to support a
2134
- # single byte, and only if ungetbyte or ungetc has not already been called on
2135
- # *ios* since the previous read of at least a single byte from *ios*. However,
2136
- # it can support additional bytes if there is space in the internal buffer to
2137
- # allow for it.
2290
+ # Pushes back ("unshifts") the given data onto the stream's buffer, placing the
2291
+ # data so that it is next to be read; returns `nil`. See [Character
2292
+ # IO](rdoc-ref:IO@Character+IO).
2293
+ #
2294
+ # Note that:
2138
2295
  #
2139
- # f = File.new("testfile") #=> #<File:testfile>
2140
- # c = f.getc #=> "8"
2141
- # f.ungetc(c) #=> nil
2142
- # f.getc #=> "8"
2296
+ # * Calling the method has no effect with unbuffered reads (such as
2297
+ # IO#sysread).
2298
+ # * Calling #rewind on the stream discards the pushed-back data.
2143
2299
  #
2144
- # If given an integer, the integer must represent a valid codepoint in the
2145
- # external encoding of *ios*.
2146
2300
  #
2147
- # Calling this method prepends to the existing buffer, even if the method has
2148
- # already been called previously:
2301
+ # When argument `integer` is given, interprets the integer as a character:
2302
+ #
2303
+ # File.write('t.tmp', '012')
2304
+ # f = File.open('t.tmp')
2305
+ # f.ungetc(0x41) # => nil
2306
+ # f.read # => "A012"
2307
+ # f.rewind
2308
+ # f.ungetc(0x0442) # => nil
2309
+ # f.getc.ord # => 1090
2310
+ # f.close
2149
2311
  #
2150
- # f = File.new("testfile") #=> #<File:testfile>
2151
- # f.ungetc("ab") #=> nil
2152
- # f.ungetc("cd") #=> nil
2153
- # f.read(5) #=> "cdab8"
2312
+ # When argument `string` is given, uses all characters:
2154
2313
  #
2155
- # Has no effect with unbuffered reads (such as IO#sysread).
2314
+ # File.write('t.tmp', '012')
2315
+ # f = File.open('t.tmp')
2316
+ # f.ungetc('A') # => nil
2317
+ # f.read # => "A012"
2318
+ # f.rewind
2319
+ # f.ungetc("\u0442\u0435\u0441\u0442") # => nil
2320
+ # f.getc.ord # => 1090
2321
+ # f.getc.ord # => 1077
2322
+ # f.getc.ord # => 1089
2323
+ # f.getc.ord # => 1090
2324
+ # f.close
2156
2325
  #
2157
2326
  def ungetc: (String arg0) -> NilClass
2158
2327
 
@@ -2161,8 +2330,9 @@ class IO < Object
2161
2330
  # - write(*objects) -> integer
2162
2331
  # -->
2163
2332
  # Writes each of the given `objects` to `self`, which must be opened for writing
2164
- # (see [Modes](#class-IO-label-Modes)); returns the total number bytes written;
2165
- # each of `objects` that is not a string is converted via method `to_s`:
2333
+ # (see [Access Modes](rdoc-ref:File@Access+Modes)); returns the total number
2334
+ # bytes written; each of `objects` that is not a string is converted via method
2335
+ # `to_s`:
2166
2336
  #
2167
2337
  # $stdout.write('Hello', ', ', 'World!', "\n") # => 14
2168
2338
  # $stdout.write('foo', :bar, 2, "\n") # => 8
@@ -2172,6 +2342,8 @@ class IO < Object
2172
2342
  # Hello, World!
2173
2343
  # foobar2
2174
2344
  #
2345
+ # Related: IO#read.
2346
+ #
2175
2347
  def write: (*_ToS string) -> Integer
2176
2348
 
2177
2349
  # <!--
@@ -2233,145 +2405,237 @@ class IO < Object
2233
2405
 
2234
2406
  # <!--
2235
2407
  # rdoc-file=io.c
2236
- # - IO.binread(name, [length [, offset]]) -> string
2237
- # - File.binread(name, [length [, offset]]) -> string
2408
+ # - IO.binread(command, length = nil, offset = 0) -> string or nil
2409
+ # - IO.binread(path, length = nil, offset = 0) -> string or nil
2238
2410
  # -->
2239
- # Opens the file, optionally seeks to the given *offset*, then returns *length*
2240
- # bytes (defaulting to the rest of the file). #binread ensures the file is
2241
- # closed before returning. The open mode would be `"rb:ASCII-8BIT"`.
2411
+ # Behaves like IO.read, except that the stream is opened in binary mode with
2412
+ # ASCII-8BIT encoding.
2242
2413
  #
2243
- # If `name` starts with a pipe character (`"|"`) and the receiver is the IO
2244
- # class, a subprocess is created in the same way as Kernel#open, and its output
2245
- # is returned. Consider to use File.binread to disable the behavior of
2246
- # subprocess invocation.
2247
- #
2248
- # File.binread("testfile") #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
2249
- # File.binread("testfile", 20) #=> "This is line one\nThi"
2250
- # File.binread("testfile", 20, 10) #=> "ne one\nThis is line "
2251
- # IO.binread("| cat testfile") #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
2252
- #
2253
- # See also IO.read for details about `name` and open_args.
2414
+ # When called from class IO (but not subclasses of IO), this method has
2415
+ # potential security vulnerabilities if called with untrusted input; see
2416
+ # [Command Injection](rdoc-ref:command_injection.rdoc).
2254
2417
  #
2255
2418
  def self.binread: (String name, ?Integer length, ?Integer offset) -> String
2256
2419
 
2257
2420
  # <!--
2258
2421
  # rdoc-file=io.c
2259
- # - IO.binwrite(name, string, [offset]) -> integer
2260
- # - IO.binwrite(name, string, [offset], open_args) -> integer
2261
- # - File.binwrite(name, string, [offset]) -> integer
2262
- # - File.binwrite(name, string, [offset], open_args) -> integer
2422
+ # - IO.binwrite(command, string, offset = 0) -> integer
2423
+ # - IO.binwrite(path, string, offset = 0) -> integer
2263
2424
  # -->
2264
- # Same as IO.write except opening the file in binary mode and ASCII-8BIT
2265
- # encoding (`"wb:ASCII-8BIT"`).
2425
+ # Behaves like IO.write, except that the stream is opened in binary mode with
2426
+ # ASCII-8BIT encoding.
2266
2427
  #
2267
- # If `name` starts with a pipe character (`"|"`) and the receiver is the IO
2268
- # class, a subprocess is created in the same way as Kernel#open, and its output
2269
- # is returned. Consider to use File.binwrite to disable the behavior of
2270
- # subprocess invocation.
2271
- #
2272
- # See also IO.read for details about `name` and open_args.
2428
+ # When called from class IO (but not subclasses of IO), this method has
2429
+ # potential security vulnerabilities if called with untrusted input; see
2430
+ # [Command Injection](rdoc-ref:command_injection.rdoc).
2273
2431
  #
2274
2432
  def self.binwrite: (String name, _ToS string, ?Integer offset, ?mode: String mode) -> Integer
2275
2433
 
2276
2434
  # <!--
2277
2435
  # rdoc-file=io.c
2278
- # - IO.copy_stream(src, dst)
2279
- # - IO.copy_stream(src, dst, copy_length)
2280
- # - IO.copy_stream(src, dst, copy_length, src_offset)
2436
+ # - IO.copy_stream(src, dst, src_length = nil, src_offset = 0) -> integer
2281
2437
  # -->
2282
- # IO.copy_stream copies *src* to *dst*. *src* and *dst* is either a filename or
2283
- # an IO-like object. IO-like object for *src* should have #readpartial or #read
2284
- # method. IO-like object for *dst* should have #write method. (Specialized
2285
- # mechanisms, such as sendfile system call, may be used on appropriate
2286
- # situation.)
2438
+ # Copies from the given `src` to the given `dst`, returning the number of bytes
2439
+ # copied.
2440
+ #
2441
+ # * The given `src` must be one of the following:
2442
+ #
2443
+ # * The path to a readable file, from which source data is to be read.
2444
+ # * An IO-like object, opened for reading and capable of responding to
2445
+ # method `:readpartial` or method `:read`.
2446
+ #
2447
+ #
2448
+ # * The given `dst` must be one of the following:
2449
+ #
2450
+ # * The path to a writable file, to which data is to be written.
2451
+ # * An IO-like object, opened for writing and capable of responding to
2452
+ # method `:write`.
2287
2453
  #
2288
- # This method returns the number of bytes copied.
2289
2454
  #
2290
- # If optional arguments are not given, the start position of the copy is the
2291
- # beginning of the filename or the current file offset of the IO. The end
2292
- # position of the copy is the end of file.
2293
2455
  #
2294
- # If *copy_length* is given, No more than *copy_length* bytes are copied.
2456
+ # The examples here use file `t.txt` as source:
2295
2457
  #
2296
- # If *src_offset* is given, it specifies the start position of the copy.
2458
+ # File.read('t.txt')
2459
+ # # => "First line\nSecond line\n\nThird line\nFourth line\n"
2460
+ # File.read('t.txt').size # => 47
2297
2461
  #
2298
- # When *src_offset* is specified and *src* is an IO, IO.copy_stream doesn't move
2299
- # the current file offset.
2462
+ # If only arguments `src` and `dst` are given, the entire source stream is
2463
+ # copied:
2464
+ #
2465
+ # # Paths.
2466
+ # IO.copy_stream('t.txt', 't.tmp') # => 47
2467
+ #
2468
+ # # IOs (recall that a File is also an IO).
2469
+ # src_io = File.open('t.txt', 'r') # => #<File:t.txt>
2470
+ # dst_io = File.open('t.tmp', 'w') # => #<File:t.tmp>
2471
+ # IO.copy_stream(src_io, dst_io) # => 47
2472
+ # src_io.close
2473
+ # dst_io.close
2474
+ #
2475
+ # With argument `src_length` a non-negative integer, no more than that many
2476
+ # bytes are copied:
2477
+ #
2478
+ # IO.copy_stream('t.txt', 't.tmp', 10) # => 10
2479
+ # File.read('t.tmp') # => "First line"
2480
+ #
2481
+ # With argument `src_offset` also given, the source stream is read beginning at
2482
+ # that offset:
2483
+ #
2484
+ # IO.copy_stream('t.txt', 't.tmp', 11, 11) # => 11
2485
+ # IO.read('t.tmp') # => "Second line"
2300
2486
  #
2301
2487
  def self.copy_stream: (String | _Reader | _ReaderPartial src, String | _Writer dst, ?Integer copy_length, ?Integer src_offset) -> Integer
2302
2488
 
2303
2489
  # <!--
2304
2490
  # rdoc-file=io.c
2305
- # - IO.popen([env,] cmd, mode="r" [, opt]) -> io
2306
- # - IO.popen([env,] cmd, mode="r" [, opt]) {|io| block } -> obj
2491
+ # - IO.popen(env = {}, cmd, mode = 'r', **opts) -> io
2492
+ # - IO.popen(env = {}, cmd, mode = 'r', **opts) {|io| ... } -> object
2307
2493
  # -->
2308
- # Runs the specified command as a subprocess; the subprocess's standard input
2309
- # and output will be connected to the returned IO object.
2494
+ # Executes the given command `cmd` as a subprocess whose $stdin and $stdout are
2495
+ # connected to a new stream `io`.
2310
2496
  #
2311
- # The PID of the started process can be obtained by IO#pid method.
2497
+ # This method has potential security vulnerabilities if called with untrusted
2498
+ # input; see [Command Injection](rdoc-ref:command_injection.rdoc).
2312
2499
  #
2313
- # *cmd* is a string or an array as follows.
2500
+ # If no block is given, returns the new stream, which depending on given `mode`
2501
+ # may be open for reading, writing, or both. The stream should be explicitly
2502
+ # closed (eventually) to avoid resource leaks.
2314
2503
  #
2315
- # cmd:
2316
- # "-" : fork
2317
- # commandline : command line string which is passed to a shell
2318
- # [env, cmdname, arg1, ..., opts] : command name and zero or more arguments (no shell)
2319
- # [env, [cmdname, argv0], arg1, ..., opts] : command name, argv[0] and zero or more arguments (no shell)
2320
- # (env and opts are optional.)
2504
+ # If a block is given, the stream is passed to the block (again, open for
2505
+ # reading, writing, or both); when the block exits, the stream is closed, and
2506
+ # the block's value is assigned to global variable `$?` and returned.
2321
2507
  #
2322
- # If *cmd* is a `String` ```-`'', then a new instance of Ruby is started as the
2323
- # subprocess.
2508
+ # Optional argument `mode` may be any valid IO mode. See [Access
2509
+ # Modes](rdoc-ref:File@Access+Modes).
2324
2510
  #
2325
- # If *cmd* is an `Array` of `String`, then it will be used as the subprocess's
2326
- # `argv` bypassing a shell. The array can contain a hash at first for
2327
- # environments and a hash at last for options similar to #spawn.
2511
+ # Required argument `cmd` determines which of the following occurs:
2328
2512
  #
2329
- # The default mode for the new file object is ``r'', but *mode* may be set to
2330
- # any of the modes listed in the description for class IO. The last argument
2331
- # *opt* qualifies *mode*.
2513
+ # * The process forks.
2514
+ # * A specified program runs in a shell.
2515
+ # * A specified program runs with specified arguments.
2516
+ # * A specified program runs with specified arguments and a specified `argv0`.
2332
2517
  #
2333
- # # set IO encoding
2334
- # IO.popen("nkf -e filename", :external_encoding=>"EUC-JP") {|nkf_io|
2335
- # euc_jp_string = nkf_io.read
2336
- # }
2337
2518
  #
2338
- # # merge standard output and standard error using
2339
- # # spawn option. See the document of Kernel.spawn.
2340
- # IO.popen(["ls", "/", :err=>[:child, :out]]) {|ls_io|
2341
- # ls_result_with_error = ls_io.read
2342
- # }
2519
+ # Each of these is detailed below.
2343
2520
  #
2344
- # # spawn options can be mixed with IO options
2345
- # IO.popen(["ls", "/"], :err=>[:child, :out]) {|ls_io|
2346
- # ls_result_with_error = ls_io.read
2347
- # }
2521
+ # The optional hash argument `env` specifies name/value pairs that are to be
2522
+ # added to the environment variables for the subprocess:
2348
2523
  #
2349
- # Raises exceptions which IO.pipe and Kernel.spawn raise.
2524
+ # IO.popen({'FOO' => 'bar'}, 'ruby', 'r+') do |pipe|
2525
+ # pipe.puts 'puts ENV["FOO"]'
2526
+ # pipe.close_write
2527
+ # pipe.gets
2528
+ # end => "bar\n"
2350
2529
  #
2351
- # If a block is given, Ruby will run the command as a child connected to Ruby
2352
- # with a pipe. Ruby's end of the pipe will be passed as a parameter to the
2353
- # block. At the end of block, Ruby closes the pipe and sets `$?`. In this case
2354
- # IO.popen returns the value of the block.
2530
+ # Optional keyword arguments `opts` specify:
2355
2531
  #
2356
- # If a block is given with a *cmd* of ```-`'', the block will be run in two
2357
- # separate processes: once in the parent, and once in a child. The parent
2358
- # process will be passed the pipe object as a parameter to the block, the child
2359
- # version of the block will be passed `nil`, and the child's standard in and
2360
- # standard out will be connected to the parent through the pipe. Not available
2361
- # on all platforms.
2532
+ # * [Open options](rdoc-ref:IO@Open+Options).
2533
+ # * [Encoding options](rdoc-ref:encodings.rdoc@Encoding+Options).
2534
+ # * Options for Kernel#spawn.
2362
2535
  #
2363
- # f = IO.popen("uname")
2364
- # p f.readlines
2365
- # f.close
2366
- # puts "Parent is #{Process.pid}"
2367
- # IO.popen("date") {|f| puts f.gets }
2368
- # IO.popen("-") {|f| $stderr.puts "#{Process.pid} is here, f is #{f.inspect}"}
2369
- # p $?
2370
- # IO.popen(%w"sed -e s|^|<foo>| -e s&$&;zot;&", "r+") {|f|
2371
- # f.puts "bar"; f.close_write; puts f.gets
2536
+ #
2537
+ # **Forked \Process**
2538
+ #
2539
+ # When argument `cmd` is the 1-character string `'-'`, causes the process to
2540
+ # fork:
2541
+ # IO.popen('-') do |pipe|
2542
+ # if pipe
2543
+ # $stderr.puts "In parent, child pid is #{pipe.pid}\n"
2544
+ # else
2545
+ # $stderr.puts "In child, pid is #{$$}\n"
2546
+ # end
2547
+ # end
2548
+ #
2549
+ # Output:
2550
+ #
2551
+ # In parent, child pid is 26253
2552
+ # In child, pid is 26253
2553
+ #
2554
+ # Note that this is not supported on all platforms.
2555
+ #
2556
+ # **Shell Subprocess**
2557
+ #
2558
+ # When argument `cmd` is a single string (but not `'-'`), the program named
2559
+ # `cmd` is run as a shell command:
2560
+ #
2561
+ # IO.popen('uname') do |pipe|
2562
+ # pipe.readlines
2563
+ # end
2564
+ #
2565
+ # Output:
2566
+ #
2567
+ # ["Linux\n"]
2568
+ #
2569
+ # Another example:
2570
+ #
2571
+ # IO.popen('/bin/sh', 'r+') do |pipe|
2572
+ # pipe.puts('ls')
2573
+ # pipe.close_write
2574
+ # $stderr.puts pipe.readlines.size
2575
+ # end
2576
+ #
2577
+ # Output:
2578
+ #
2579
+ # 213
2580
+ #
2581
+ # **Program Subprocess**
2582
+ #
2583
+ # When argument `cmd` is an array of strings, the program named `cmd[0]` is run
2584
+ # with all elements of `cmd` as its arguments:
2585
+ #
2586
+ # IO.popen(['du', '..', '.']) do |pipe|
2587
+ # $stderr.puts pipe.readlines.size
2588
+ # end
2589
+ #
2590
+ # Output:
2591
+ #
2592
+ # 1111
2593
+ #
2594
+ # **Program Subprocess with `argv0`**
2595
+ #
2596
+ # When argument `cmd` is an array whose first element is a 2-element string
2597
+ # array and whose remaining elements (if any) are strings:
2598
+ #
2599
+ # * `cmd[0][0]` (the first string in the nested array) is the name of a
2600
+ # program that is run.
2601
+ # * `cmd[0][1]` (the second string in the nested array) is set as the
2602
+ # program's `argv[0]`.
2603
+ # * `cmd[1..-1]` (the strings in the outer array) are the program's arguments.
2604
+ #
2605
+ #
2606
+ # Example (sets `$0` to 'foo'):
2607
+ #
2608
+ # IO.popen([['/bin/sh', 'foo'], '-c', 'echo $0']).read # => "foo\n"
2609
+ #
2610
+ # **Some Special Examples**
2611
+ #
2612
+ # # Set IO encoding.
2613
+ # IO.popen("nkf -e filename", :external_encoding=>"EUC-JP") {|nkf_io|
2614
+ # euc_jp_string = nkf_io.read
2372
2615
  # }
2373
2616
  #
2374
- # *produces:*
2617
+ # # Merge standard output and standard error using Kernel#spawn option. See Kernel#spawn.
2618
+ # IO.popen(["ls", "/", :err=>[:child, :out]]) do |io|
2619
+ # ls_result_with_error = io.read
2620
+ # end
2621
+ #
2622
+ # # Use mixture of spawn options and IO options.
2623
+ # IO.popen(["ls", "/"], :err=>[:child, :out]) do |io|
2624
+ # ls_result_with_error = io.read
2625
+ # end
2626
+ #
2627
+ # f = IO.popen("uname")
2628
+ # p f.readlines
2629
+ # f.close
2630
+ # puts "Parent is #{Process.pid}"
2631
+ # IO.popen("date") {|f| puts f.gets }
2632
+ # IO.popen("-") {|f| $stderr.puts "#{Process.pid} is here, f is #{f.inspect}"}
2633
+ # p $?
2634
+ # IO.popen(%w"sed -e s|^|<foo>| -e s&$&;zot;&", "r+") {|f|
2635
+ # f.puts "bar"; f.close_write; puts f.gets
2636
+ # }
2637
+ #
2638
+ # Output (from last section):
2375
2639
  #
2376
2640
  # ["Linux\n"]
2377
2641
  # Parent is 21346
@@ -2381,79 +2645,162 @@ class IO < Object
2381
2645
  # #<Process::Status: pid 21352 exit 0>
2382
2646
  # <foo>bar;zot;
2383
2647
  #
2648
+ # Raises exceptions that IO.pipe and Kernel.spawn raise.
2649
+ #
2384
2650
  def self.popen: (*untyped args) -> untyped
2385
2651
 
2386
2652
  # <!--
2387
2653
  # rdoc-file=io.c
2388
- # - IO.foreach(name, sep=$/ [, getline_args, open_args]) {|line| block } -> nil
2389
- # - IO.foreach(name, limit [, getline_args, open_args]) {|line| block } -> nil
2390
- # - IO.foreach(name, sep, limit [, getline_args, open_args]) {|line| block } -> nil
2391
- # - IO.foreach(...) -> an_enumerator
2392
- # - File.foreach(name, sep=$/ [, getline_args, open_args]) {|line| block } -> nil
2393
- # - File.foreach(name, limit [, getline_args, open_args]) {|line| block } -> nil
2394
- # - File.foreach(name, sep, limit [, getline_args, open_args]) {|line| block } -> nil
2395
- # - File.foreach(...) -> an_enumerator
2654
+ # - IO.foreach(path, sep = $/, **opts) {|line| block } -> nil
2655
+ # - IO.foreach(path, limit, **opts) {|line| block } -> nil
2656
+ # - IO.foreach(path, sep, limit, **opts) {|line| block } -> nil
2657
+ # - IO.foreach(command, sep = $/, **opts) {|line| block } -> nil
2658
+ # - IO.foreach(command, limit, **opts) {|line| block } -> nil
2659
+ # - IO.foreach(command, sep, limit, **opts) {|line| block } -> nil
2660
+ # - IO.foreach(...) -> an_enumerator
2396
2661
  # -->
2397
- # Executes the block for every line in the named I/O port, where lines are
2398
- # separated by *sep*.
2662
+ # Calls the block with each successive line read from the stream.
2399
2663
  #
2400
- # If no block is given, an enumerator is returned instead.
2664
+ # When called from class IO (but not subclasses of IO), this method has
2665
+ # potential security vulnerabilities if called with untrusted input; see
2666
+ # [Command Injection](rdoc-ref:command_injection.rdoc).
2401
2667
  #
2402
- # If `name` starts with a pipe character (`"|"`) and the receiver is the IO
2403
- # class, a subprocess is created in the same way as Kernel#open, and its output
2404
- # is returned. Consider to use File.foreach to disable the behavior of
2405
- # subprocess invocation.
2668
+ # The first argument must be a string that is one of the following:
2406
2669
  #
2407
- # File.foreach("testfile") {|x| print "GOT ", x }
2408
- # IO.foreach("| cat testfile") {|x| print "GOT ", x }
2670
+ # * Path: if `self` is a subclass of IO (File, for example), or if the string
2671
+ # *does* *not* start with the pipe character (`'|'`), the string is the path
2672
+ # to a file.
2673
+ # * Command: if `self` is the class IO, and if the string starts with the pipe
2674
+ # character, the rest of the string is a command to be executed as a
2675
+ # subprocess. This usage has potential security vulnerabilities if called
2676
+ # with untrusted input; see [Command
2677
+ # Injection](rdoc-ref:command_injection.rdoc).
2409
2678
  #
2410
- # *produces:*
2411
2679
  #
2412
- # GOT This is line one
2413
- # GOT This is line two
2414
- # GOT This is line three
2415
- # GOT And so on...
2680
+ # With only argument `path` given, parses lines from the file at the given
2681
+ # `path`, as determined by the default line separator, and calls the block with
2682
+ # each successive line:
2683
+ #
2684
+ # File.foreach('t.txt') {|line| p line }
2685
+ #
2686
+ # Output: the same as above.
2687
+ #
2688
+ # For both forms, command and path, the remaining arguments are the same.
2689
+ #
2690
+ # With argument `sep` given, parses lines as determined by that line separator
2691
+ # (see [Line Separator](rdoc-ref:IO@Line+Separator)):
2692
+ #
2693
+ # File.foreach('t.txt', 'li') {|line| p line }
2694
+ #
2695
+ # Output:
2696
+ #
2697
+ # "First li"
2698
+ # "ne\nSecond li"
2699
+ # "ne\n\nThird li"
2700
+ # "ne\nFourth li"
2701
+ # "ne\n"
2702
+ #
2703
+ # Each paragraph:
2704
+ #
2705
+ # File.foreach('t.txt', '') {|paragraph| p paragraph }
2706
+ #
2707
+ # Output:
2708
+ #
2709
+ # "First line\nSecond line\n\n"
2710
+ # "Third line\nFourth line\n"
2711
+ #
2712
+ # With argument `limit` given, parses lines as determined by the default line
2713
+ # separator and the given line-length limit (see [Line
2714
+ # Limit](rdoc-ref:IO@Line+Limit)):
2715
+ #
2716
+ # File.foreach('t.txt', 7) {|line| p line }
2717
+ #
2718
+ # Output:
2719
+ #
2720
+ # "First l"
2721
+ # "ine\n"
2722
+ # "Second "
2723
+ # "line\n"
2724
+ # "\n"
2725
+ # "Third l"
2726
+ # "ine\n"
2727
+ # "Fourth l"
2728
+ # "line\n"
2729
+ #
2730
+ # With arguments `sep` and `limit` given, parses lines as determined by the
2731
+ # given line separator and the given line-length limit (see [Line Separator and
2732
+ # Line Limit](rdoc-ref:IO@Line+Separator+and+Line+Limit)):
2733
+ #
2734
+ # Optional keyword arguments `opts` specify:
2735
+ #
2736
+ # * [Open Options](rdoc-ref:IO@Open+Options).
2737
+ # * [Encoding options](rdoc-ref:encodings.rdoc@Encoding+Options).
2738
+ # * [Line Options](rdoc-ref:IO@Line+Options).
2416
2739
  #
2417
- # If the last argument is a hash, it's the keyword argument to open. See
2418
- # IO.readlines for details about getline_args. And see also IO.read for details
2419
- # about open_args.
2740
+ #
2741
+ # Returns an Enumerator if no block is given.
2420
2742
  #
2421
2743
  def self.foreach: (string | _ToPath path, ?String sep, ?Integer limit, ?external_encoding: String external_encoding, ?internal_encoding: String internal_encoding, ?encoding: String encoding, ?textmode: untyped textmode, ?binmode: untyped binmode, ?autoclose: untyped autoclose, ?mode: String mode, ?chomp: boolish) { (String line) -> void } -> nil
2422
2744
  | (string | _ToPath path, ?String sep, ?Integer limit, ?external_encoding: String external_encoding, ?internal_encoding: String internal_encoding, ?encoding: String encoding, ?textmode: untyped textmode, ?binmode: untyped binmode, ?autoclose: untyped autoclose, ?mode: String mode, ?chomp: boolish) -> ::Enumerator[String, nil]
2423
2745
 
2424
2746
  # <!--
2425
2747
  # rdoc-file=io.c
2426
- # - IO.pipe -> [read_io, write_io]
2427
- # - IO.pipe(ext_enc) -> [read_io, write_io]
2428
- # - IO.pipe("ext_enc:int_enc" [, opt]) -> [read_io, write_io]
2429
- # - IO.pipe(ext_enc, int_enc [, opt]) -> [read_io, write_io]
2430
- # - IO.pipe(...) {|read_io, write_io| ... }
2748
+ # - IO.pipe(**opts) -> [read_io, write_io]
2749
+ # - IO.pipe(enc, **opts) -> [read_io, write_io]
2750
+ # - IO.pipe(ext_enc, int_enc, **opts) -> [read_io, write_io]
2751
+ # - IO.pipe(**opts) {|read_io, write_io] ...} -> object
2752
+ # - IO.pipe(enc, **opts) {|read_io, write_io] ...} -> object
2753
+ # - IO.pipe(ext_enc, int_enc, **opts) {|read_io, write_io] ...} -> object
2431
2754
  # -->
2432
- # Creates a pair of pipe endpoints (connected to each other) and returns them as
2433
- # a two-element array of IO objects: `[` *read_io*, *write_io* `]`.
2755
+ # Creates a pair of pipe endpoints, `read_io` and `write_io`, connected to each
2756
+ # other.
2434
2757
  #
2435
- # If a block is given, the block is called and returns the value of the block.
2436
- # *read_io* and *write_io* are sent to the block as arguments. If read_io and
2437
- # write_io are not closed when the block exits, they are closed. i.e. closing
2438
- # read_io and/or write_io doesn't cause an error.
2758
+ # If argument `enc_string` is given, it must be a string containing one of:
2439
2759
  #
2440
- # Not available on all platforms.
2760
+ # * The name of the encoding to be used as the external encoding.
2761
+ # * The colon-separated names of two encodings to be used as the external and
2762
+ # internal encodings.
2763
+ #
2764
+ #
2765
+ # If argument `int_enc` is given, it must be an Encoding object or encoding name
2766
+ # string that specifies the internal encoding to be used; if argument `ext_enc`
2767
+ # is also given, it must be an Encoding object or encoding name string that
2768
+ # specifies the external encoding to be used.
2769
+ #
2770
+ # The string read from `read_io` is tagged with the external encoding; if an
2771
+ # internal encoding is also specified, the string is converted to, and tagged
2772
+ # with, that encoding.
2773
+ #
2774
+ # If any encoding is specified, optional hash arguments specify the conversion
2775
+ # option.
2776
+ #
2777
+ # Optional keyword arguments `opts` specify:
2778
+ #
2779
+ # * [Open Options](rdoc-ref:IO@Open+Options).
2780
+ # * [Encoding Options](rdoc-ref:encodings.rdoc@Encoding+Options).
2781
+ #
2782
+ #
2783
+ # With no block given, returns the two endpoints in an array:
2784
+ #
2785
+ # IO.pipe # => [#<IO:fd 4>, #<IO:fd 5>]
2786
+ #
2787
+ # With a block given, calls the block with the two endpoints; closes both
2788
+ # endpoints and returns the value of the block:
2789
+ #
2790
+ # IO.pipe {|read_io, write_io| p read_io; p write_io }
2791
+ #
2792
+ # Output:
2793
+ #
2794
+ # #<IO:fd 6>
2795
+ # #<IO:fd 7>
2441
2796
  #
2442
- # If an encoding (encoding name or encoding object) is specified as an optional
2443
- # argument, read string from pipe is tagged with the encoding specified. If the
2444
- # argument is a colon separated two encoding names "A:B", the read string is
2445
- # converted from encoding A (external encoding) to encoding B (internal
2446
- # encoding), then tagged with B. If two optional arguments are specified, those
2447
- # must be encoding objects or encoding names, and the first one is the external
2448
- # encoding, and the second one is the internal encoding. If the external
2449
- # encoding and the internal encoding is specified, optional hash argument
2450
- # specify the conversion option.
2797
+ # Not available on all platforms.
2451
2798
  #
2452
2799
  # In the example below, the two processes close the ends of the pipe that they
2453
2800
  # are not using. This is not just a cosmetic nicety. The read end of a pipe will
2454
2801
  # not generate an end of file condition if there are any writers with the pipe
2455
2802
  # still open. In the case of the parent process, the `rd.read` will never return
2456
- # if it does not first issue a `wr.close`.
2803
+ # if it does not first issue a `wr.close`:
2457
2804
  #
2458
2805
  # rd, wr = IO.pipe
2459
2806
  #
@@ -2464,7 +2811,7 @@ class IO < Object
2464
2811
  # Process.wait
2465
2812
  # else
2466
2813
  # rd.close
2467
- # puts "Sending message to parent"
2814
+ # puts 'Sending message to parent'
2468
2815
  # wr.write "Hi Dad"
2469
2816
  # wr.close
2470
2817
  # end
@@ -2479,113 +2826,168 @@ class IO < Object
2479
2826
 
2480
2827
  # <!--
2481
2828
  # rdoc-file=io.c
2482
- # - IO.read(name, [length [, offset]] [, opt]) -> string
2483
- # - File.read(name, [length [, offset]] [, opt]) -> string
2829
+ # - IO.read(command, length = nil, offset = 0, **opts) -> string or nil
2830
+ # - IO.read(path, length = nil, offset = 0, **opts) -> string or nil
2484
2831
  # -->
2485
- # Opens the file, optionally seeks to the given `offset`, then returns `length`
2486
- # bytes (defaulting to the rest of the file). #read ensures the file is closed
2487
- # before returning.
2832
+ # Opens the stream, reads and returns some or all of its content, and closes the
2833
+ # stream; returns `nil` if no bytes were read.
2488
2834
  #
2489
- # If `name` starts with a pipe character (`"|"`) and the receiver is the IO
2490
- # class, a subprocess is created in the same way as Kernel#open, and its output
2491
- # is returned. Consider to use File.read to disable the behavior of subprocess
2492
- # invocation.
2835
+ # When called from class IO (but not subclasses of IO), this method has
2836
+ # potential security vulnerabilities if called with untrusted input; see
2837
+ # [Command Injection](rdoc-ref:command_injection.rdoc).
2493
2838
  #
2494
- # ### Options
2839
+ # The first argument must be a string; its meaning depends on whether it starts
2840
+ # with the pipe character (`'|'`):
2495
2841
  #
2496
- # The options hash accepts the following keys:
2842
+ # * If so (and if `self` is IO), the rest of the string is a command to be
2843
+ # executed as a subprocess.
2844
+ # * Otherwise, the string is the path to a file.
2497
2845
  #
2498
- # :encoding
2499
- # : string or encoding
2500
2846
  #
2501
- # Specifies the encoding of the read string. `:encoding` will be ignored if
2502
- # `length` is specified. See Encoding.aliases for possible encodings.
2847
+ # With only argument `command` given, executes the command in a shell, returns
2848
+ # its entire $stdout:
2503
2849
  #
2504
- # :mode
2505
- # : string or integer
2850
+ # IO.read('| cat t.txt')
2851
+ # # => "First line\nSecond line\n\nThird line\nFourth line\n"
2506
2852
  #
2507
- # Specifies the *mode* argument for open(). It must start with an "r",
2508
- # otherwise it will cause an error. See IO.new for the list of possible
2509
- # modes.
2853
+ # With only argument `path` given, reads in text mode and returns the entire
2854
+ # content of the file at the given path:
2510
2855
  #
2511
- # :open_args
2512
- # : array
2856
+ # IO.read('t.txt')
2857
+ # # => "First line\nSecond line\n\nThird line\nFourth line\n"
2513
2858
  #
2514
- # Specifies arguments for open() as an array. This key can not be used in
2515
- # combination with either `:encoding` or `:mode`.
2859
+ # On Windows, text mode can terminate reading and leave bytes in the file unread
2860
+ # when encountering certain special bytes. Consider using IO.binread if all
2861
+ # bytes in the file should be read.
2516
2862
  #
2863
+ # For both forms, command and path, the remaining arguments are the same.
2517
2864
  #
2518
- # Examples:
2865
+ # With argument `length`, returns `length` bytes if available:
2866
+ #
2867
+ # IO.read('t.txt', 7) # => "First l"
2868
+ # IO.read('t.txt', 700)
2869
+ # # => "First line\r\nSecond line\r\n\r\nFourth line\r\nFifth line\r\n"
2870
+ #
2871
+ # With arguments `length` and `offset`, returns `length` bytes if available,
2872
+ # beginning at the given `offset`:
2519
2873
  #
2520
- # File.read("testfile") #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
2521
- # File.read("testfile", 20) #=> "This is line one\nThi"
2522
- # File.read("testfile", 20, 10) #=> "ne one\nThis is line "
2523
- # File.read("binfile", mode: "rb") #=> "\xF7\x00\x00\x0E\x12"
2524
- # IO.read("|ls -a") #=> ".\n..\n"...
2874
+ # IO.read('t.txt', 10, 2) # => "rst line\nS"
2875
+ # IO.read('t.txt', 10, 200) # => nil
2876
+ #
2877
+ # Optional keyword arguments `opts` specify:
2878
+ #
2879
+ # * [Open Options](rdoc-ref:IO@Open+Options).
2880
+ # * [Encoding options](rdoc-ref:encodings.rdoc@Encoding+Options).
2525
2881
  #
2526
2882
  def self.read: (String name, ?Integer length, ?Integer offset, ?external_encoding: String external_encoding, ?internal_encoding: String internal_encoding, ?encoding: String encoding, ?textmode: untyped textmode, ?binmode: untyped binmode, ?autoclose: untyped autoclose, ?mode: String mode) -> String
2527
2883
 
2528
2884
  # <!--
2529
2885
  # rdoc-file=io.c
2530
- # - IO.readlines(name, sep=$/ [, getline_args, open_args]) -> array
2531
- # - IO.readlines(name, limit [, getline_args, open_args]) -> array
2532
- # - IO.readlines(name, sep, limit [, getline_args, open_args]) -> array
2533
- # - File.readlines(name, sep=$/ [, getline_args, open_args]) -> array
2534
- # - File.readlines(name, limit [, getline_args, open_args]) -> array
2535
- # - File.readlines(name, sep, limit [, getline_args, open_args]) -> array
2886
+ # - IO.readlines(command, sep = $/, **opts) -> array
2887
+ # - IO.readlines(command, limit, **opts) -> array
2888
+ # - IO.readlines(command, sep, limit, **opts) -> array
2889
+ # - IO.readlines(path, sep = $/, **opts) -> array
2890
+ # - IO.readlines(path, limit, **opts) -> array
2891
+ # - IO.readlines(path, sep, limit, **opts) -> array
2536
2892
  # -->
2537
- # Reads the entire file specified by *name* as individual lines, and returns
2538
- # those lines in an array. Lines are separated by *sep*.
2893
+ # Returns an array of all lines read from the stream.
2894
+ #
2895
+ # When called from class IO (but not subclasses of IO), this method has
2896
+ # potential security vulnerabilities if called with untrusted input; see
2897
+ # [Command Injection](rdoc-ref:command_injection.rdoc).
2898
+ #
2899
+ # The first argument must be a string; its meaning depends on whether it starts
2900
+ # with the pipe character (`'|'`):
2539
2901
  #
2540
- # If `name` starts with a pipe character (`"|"`) and the receiver is the IO
2541
- # class, a subprocess is created in the same way as Kernel#open, and its output
2542
- # is returned. Consider to use File.readlines to disable the behavior of
2543
- # subprocess invocation.
2902
+ # * If so (and if `self` is IO), the rest of the string is a command to be
2903
+ # executed as a subprocess.
2904
+ # * Otherwise, the string is the path to a file.
2544
2905
  #
2545
- # a = File.readlines("testfile")
2546
- # a[0] #=> "This is line one\n"
2547
2906
  #
2548
- # b = File.readlines("testfile", chomp: true)
2549
- # b[0] #=> "This is line one"
2907
+ # With only argument `command` given, executes the command in a shell, parses
2908
+ # its $stdout into lines, as determined by the default line separator, and
2909
+ # returns those lines in an array:
2550
2910
  #
2551
- # IO.readlines("|ls -a") #=> [".\n", "..\n", ...]
2911
+ # IO.readlines('| cat t.txt')
2912
+ # # => ["First line\n", "Second line\n", "\n", "Third line\n", "Fourth line\n"]
2552
2913
  #
2553
- # If the last argument is a hash, it's the keyword argument to open.
2914
+ # With only argument `path` given, parses lines from the file at the given
2915
+ # `path`, as determined by the default line separator, and returns those lines
2916
+ # in an array:
2554
2917
  #
2555
- # ### Options for getline
2918
+ # IO.readlines('t.txt')
2919
+ # # => ["First line\n", "Second line\n", "\n", "Third line\n", "Fourth line\n"]
2556
2920
  #
2557
- # The options hash accepts the following keys:
2921
+ # For both forms, command and path, the remaining arguments are the same.
2558
2922
  #
2559
- # :chomp
2560
- # : When the optional `chomp` keyword argument has a true value, `\n`, `\r`,
2561
- # and `\r\n` will be removed from the end of each line.
2923
+ # With argument `sep` given, parses lines as determined by that line separator
2924
+ # (see [Line Separator](rdoc-ref:IO@Line+Separator)):
2562
2925
  #
2926
+ # # Ordinary separator.
2927
+ # IO.readlines('t.txt', 'li')
2928
+ # # =>["First li", "ne\nSecond li", "ne\n\nThird li", "ne\nFourth li", "ne\n"]
2929
+ # # Get-paragraphs separator.
2930
+ # IO.readlines('t.txt', '')
2931
+ # # => ["First line\nSecond line\n\n", "Third line\nFourth line\n"]
2932
+ # # Get-all separator.
2933
+ # IO.readlines('t.txt', nil)
2934
+ # # => ["First line\nSecond line\n\nThird line\nFourth line\n"]
2563
2935
  #
2564
- # See also IO.read for details about `name` and open_args.
2936
+ # With argument `limit` given, parses lines as determined by the default line
2937
+ # separator and the given line-length limit (see [Line
2938
+ # Limit](rdoc-ref:IO@Line+Limit)):
2939
+ #
2940
+ # IO.readlines('t.txt', 7)
2941
+ # # => ["First l", "ine\n", "Second ", "line\n", "\n", "Third l", "ine\n", "Fourth ", "line\n"]
2942
+ #
2943
+ # With arguments `sep` and `limit` given, parses lines as determined by the
2944
+ # given line separator and the given line-length limit (see [Line Separator and
2945
+ # Line Limit](rdoc-ref:IO@Line+Separator+and+Line+Limit)):
2946
+ #
2947
+ # Optional keyword arguments `opts` specify:
2948
+ #
2949
+ # * [Open Options](rdoc-ref:IO@Open+Options).
2950
+ # * [Encoding options](rdoc-ref:encodings.rdoc@Encoding+Options).
2951
+ # * [Line Options](rdoc-ref:IO@Line+Options).
2565
2952
  #
2566
2953
  def self.readlines: (String | _ToPath name, ?String sep, ?Integer limit, ?external_encoding: String external_encoding, ?internal_encoding: String internal_encoding, ?encoding: String encoding, ?textmode: untyped textmode, ?binmode: untyped binmode, ?autoclose: untyped autoclose, ?mode: String mode, ?chomp: boolish) -> ::Array[String]
2567
2954
 
2568
2955
  # <!--
2569
2956
  # rdoc-file=io.c
2570
- # - IO.select(read_array [, write_array [, error_array [, timeout]]]) -> array or nil
2957
+ # - IO.select(read_ios, write_ios = [], error_ios = [], timeout = nil) -> array or nil
2571
2958
  # -->
2572
- # Calls select(2) system call. It monitors given arrays of IO objects, waits
2573
- # until one or more of IO objects are ready for reading, are ready for writing,
2574
- # and have pending exceptions respectively, and returns an array that contains
2575
- # arrays of those IO objects. It will return `nil` if optional *timeout* value
2576
- # is given and no IO object is ready in *timeout* seconds.
2959
+ # Invokes system call [select(2)](https://linux.die.net/man/2/select), which
2960
+ # monitors multiple file descriptors, waiting until one or more of the file
2961
+ # descriptors becomes ready for some class of I/O operation.
2962
+ #
2963
+ # Not implemented on all platforms.
2964
+ #
2965
+ # Each of the arguments `read_ios`, `write_ios`, and `error_ios` is an array of
2966
+ # IO objects.
2967
+ #
2968
+ # Argument `timeout` is an integer timeout interval in seconds.
2969
+ #
2970
+ # The method monitors the IO objects given in all three arrays, waiting for some
2971
+ # to be ready; returns a 3-element array whose elements are:
2972
+ #
2973
+ # * An array of the objects in `read_ios` that are ready for reading.
2974
+ # * An array of the objects in `write_ios` that are ready for writing.
2975
+ # * An array of the objects in `error_ios` have pending exceptions.
2976
+ #
2977
+ #
2978
+ # If no object becomes ready within the given `timeout`, `nil` is returned.
2577
2979
  #
2578
2980
  # IO.select peeks the buffer of IO objects for testing readability. If the IO
2579
2981
  # buffer is not empty, IO.select immediately notifies readability. This "peek"
2580
2982
  # only happens for IO objects. It does not happen for IO-like objects such as
2581
2983
  # OpenSSL::SSL::SSLSocket.
2582
2984
  #
2583
- # The best way to use IO.select is invoking it after nonblocking methods such as
2584
- # #read_nonblock, #write_nonblock, etc. The methods raise an exception which is
2585
- # extended by IO::WaitReadable or IO::WaitWritable. The modules notify how the
2586
- # caller should wait with IO.select. If IO::WaitReadable is raised, the caller
2587
- # should wait for reading. If IO::WaitWritable is raised, the caller should
2588
- # wait for writing.
2985
+ # The best way to use IO.select is invoking it after non-blocking methods such
2986
+ # as #read_nonblock, #write_nonblock, etc. The methods raise an exception which
2987
+ # is extended by IO::WaitReadable or IO::WaitWritable. The modules notify how
2988
+ # the caller should wait with IO.select. If IO::WaitReadable is raised, the
2989
+ # caller should wait for reading. If IO::WaitWritable is raised, the caller
2990
+ # should wait for writing.
2589
2991
  #
2590
2992
  # So, blocking read (#readpartial) can be emulated using #read_nonblock and
2591
2993
  # IO.select as follows:
@@ -2600,7 +3002,7 @@ class IO < Object
2600
3002
  # retry
2601
3003
  # end
2602
3004
  #
2603
- # Especially, the combination of nonblocking methods and IO.select is preferred
3005
+ # Especially, the combination of non-blocking methods and IO.select is preferred
2604
3006
  # for IO like objects such as OpenSSL::SSL::SSLSocket. It has #to_io method to
2605
3007
  # return underlying IO object. IO.select calls #to_io to obtain the file
2606
3008
  # descriptor to wait.
@@ -2626,13 +3028,13 @@ class IO < Object
2626
3028
  # blocking. So, the caller should wait for ready for writability as above
2627
3029
  # example.
2628
3030
  #
2629
- # The combination of nonblocking methods and IO.select is also useful for
3031
+ # The combination of non-blocking methods and IO.select is also useful for
2630
3032
  # streams such as tty, pipe socket socket when multiple processes read from a
2631
3033
  # stream.
2632
3034
  #
2633
3035
  # Finally, Linux kernel developers don't guarantee that readability of select(2)
2634
- # means readability of following read(2) even for a single process. See
2635
- # select(2) manual on GNU/Linux system.
3036
+ # means readability of following read(2) even for a single process; see
3037
+ # [select(2)](https://linux.die.net/man/2/select)
2636
3038
  #
2637
3039
  # Invoking IO.select before IO#readpartial works well as usual. However it is
2638
3040
  # not the best way to use IO.select.
@@ -2659,18 +3061,7 @@ class IO < Object
2659
3061
  # string = string.byteslice(written..-1)
2660
3062
  # end
2661
3063
  #
2662
- # ### Parameters
2663
- # read_array
2664
- # : an array of IO objects that wait until ready for read
2665
- # write_array
2666
- # : an array of IO objects that wait until ready for write
2667
- # error_array
2668
- # : an array of IO objects that wait for exceptions
2669
- # timeout
2670
- # : a numeric value in second
2671
- #
2672
- #
2673
- # ### Example
3064
+ # Example:
2674
3065
  #
2675
3066
  # rp, wp = IO.pipe
2676
3067
  # mesg = "ping "
@@ -2692,7 +3083,7 @@ class IO < Object
2692
3083
  # end
2693
3084
  # }
2694
3085
  #
2695
- # *produces:*
3086
+ # Output:
2696
3087
  #
2697
3088
  # ping pong
2698
3089
  # ping pong
@@ -2705,11 +3096,17 @@ class IO < Object
2705
3096
 
2706
3097
  # <!--
2707
3098
  # rdoc-file=io.c
2708
- # - IO.sysopen(path, [mode, [perm]]) -> integer
3099
+ # - IO.sysopen(path, mode = 'r', perm = 0666) -> integer
2709
3100
  # -->
2710
- # Opens the given path, returning the underlying file descriptor as a Integer.
3101
+ # Opens the file at the given path with the given mode and permissions; returns
3102
+ # the integer file descriptor.
3103
+ #
3104
+ # If the file is to be readable, it must exist; if the file is to be writable
3105
+ # and does not exist, it is created with the given permissions:
2711
3106
  #
2712
- # IO.sysopen("testfile") #=> 3
3107
+ # File.write('t.tmp', '') # => 0
3108
+ # IO.sysopen('t.tmp') # => 8
3109
+ # IO.sysopen('t.tmp', 'w') # => 9
2713
3110
  #
2714
3111
  def self.sysopen: (String path, ?String mode, ?String perm) -> Integer
2715
3112
 
@@ -2728,96 +3125,87 @@ class IO < Object
2728
3125
 
2729
3126
  # <!--
2730
3127
  # rdoc-file=io.c
2731
- # - IO.write(name, string [, offset]) -> integer
2732
- # - IO.write(name, string [, offset] [, opt]) -> integer
2733
- # - File.write(name, string [, offset]) -> integer
2734
- # - File.write(name, string [, offset] [, opt]) -> integer
3128
+ # - IO.write(command, data, **opts) -> integer
3129
+ # - IO.write(path, data, offset = 0, **opts) -> integer
2735
3130
  # -->
2736
- # Opens the file, optionally seeks to the given *offset*, writes *string*, then
2737
- # returns the length written. #write ensures the file is closed before
2738
- # returning. If *offset* is not given in write mode, the file is truncated.
2739
- # Otherwise, it is not truncated.
3131
+ # Opens the stream, writes the given `data` to it, and closes the stream;
3132
+ # returns the number of bytes written.
2740
3133
  #
2741
- # If `name` starts with a pipe character (`"|"`) and the receiver is the IO
2742
- # class, a subprocess is created in the same way as Kernel#open, and its output
2743
- # is returned. Consider to use File.write to disable the behavior of subprocess
2744
- # invocation.
3134
+ # When called from class IO (but not subclasses of IO), this method has
3135
+ # potential security vulnerabilities if called with untrusted input; see
3136
+ # [Command Injection](rdoc-ref:command_injection.rdoc).
2745
3137
  #
2746
- # File.write("testfile", "0123456789", 20) #=> 10
2747
- # # File could contain: "This is line one\nThi0123456789two\nThis is line three\nAnd so on...\n"
2748
- # File.write("testfile", "0123456789") #=> 10
2749
- # # File would now read: "0123456789"
2750
- # IO.write("|tr a-z A-Z", "abc") #=> 3
2751
- # # Prints "ABC" to the standard output
3138
+ # The first argument must be a string; its meaning depends on whether it starts
3139
+ # with the pipe character (`'|'`):
2752
3140
  #
2753
- # If the last argument is a hash, it specifies options for the internal open().
2754
- # It accepts the following keys:
3141
+ # * If so (and if `self` is IO), the rest of the string is a command to be
3142
+ # executed as a subprocess.
3143
+ # * Otherwise, the string is the path to a file.
2755
3144
  #
2756
- # :encoding
2757
- # : string or encoding
2758
3145
  #
2759
- # Specifies the encoding of the read string. See Encoding.aliases for
2760
- # possible encodings.
3146
+ # With argument `command` given, executes the command in a shell, passes `data`
3147
+ # through standard input, writes its output to $stdout, and returns the length
3148
+ # of the given `data`:
2761
3149
  #
2762
- # :mode
2763
- # : string or integer
3150
+ # IO.write('| cat', 'Hello World!') # => 12
2764
3151
  #
2765
- # Specifies the *mode* argument for open(). It must start with "w", "a", or
2766
- # "r+", otherwise it will cause an error. See IO.new for the list of
2767
- # possible modes.
3152
+ # Output:
3153
+ #
3154
+ # Hello World!
3155
+ #
3156
+ # With argument `path` given, writes the given `data` to the file at that path:
3157
+ #
3158
+ # IO.write('t.tmp', 'abc') # => 3
3159
+ # File.read('t.tmp') # => "abc"
2768
3160
  #
2769
- # :perm
2770
- # : integer
3161
+ # If `offset` is zero (the default), the file is overwritten:
2771
3162
  #
2772
- # Specifies the *perm* argument for open().
3163
+ # IO.write('t.tmp', 'A') # => 1
3164
+ # File.read('t.tmp') # => "A"
2773
3165
  #
2774
- # :open_args
2775
- # : array
3166
+ # If `offset` in within the file content, the file is partly overwritten:
2776
3167
  #
2777
- # Specifies arguments for open() as an array. This key can not be used in
2778
- # combination with other keys.
3168
+ # IO.write('t.tmp', 'abcdef') # => 3
3169
+ # File.read('t.tmp') # => "abcdef"
3170
+ # # Offset within content.
3171
+ # IO.write('t.tmp', '012', 2) # => 3
3172
+ # File.read('t.tmp') # => "ab012f"
2779
3173
  #
3174
+ # If `offset` is outside the file content, the file is padded with null
3175
+ # characters `"\u0000"`:
2780
3176
  #
2781
- # See also IO.read for details about `name` and open_args.
3177
+ # IO.write('t.tmp', 'xyz', 10) # => 3
3178
+ # File.read('t.tmp') # => "ab012f\u0000\u0000\u0000\u0000xyz"
3179
+ #
3180
+ # Optional keyword arguments `opts` specify:
3181
+ #
3182
+ # * [Open Options](rdoc-ref:IO@Open+Options).
3183
+ # * [Encoding options](rdoc-ref:encodings.rdoc@Encoding+Options).
2782
3184
  #
2783
3185
  def self.write: (String name, _ToS arg0, ?Integer offset, ?external_encoding: String external_encoding, ?internal_encoding: String internal_encoding, ?encoding: String encoding, ?textmode: untyped textmode, ?binmode: untyped binmode, ?autoclose: untyped autoclose, ?mode: String mode) -> Integer
2784
3186
 
2785
3187
  # <!--
2786
3188
  # rdoc-file=io.c
2787
- # - IO.for_fd(fd, mode [, opt]) -> io
3189
+ # - IO.for_fd(fd, mode = 'r', **opts) -> io
2788
3190
  # -->
2789
3191
  # Synonym for IO.new.
2790
3192
  #
2791
- def self.for_fd: (int fd, ?string | int mode, **untyped opt) -> instance
3193
+ alias self.for_fd self.new
2792
3194
 
2793
3195
  # <!--
2794
3196
  # rdoc-file=io.c
2795
- # - IO.open(fd, mode="r" [, opt]) -> io
2796
- # - IO.open(fd, mode="r" [, opt]) {|io| block } -> obj
3197
+ # - IO.open(fd, mode = 'r', **opts) -> io
3198
+ # - IO.open(fd, mode = 'r', **opts) {|io| ... } -> object
2797
3199
  # -->
2798
- # With no associated block, IO.open is a synonym for IO.new. If the optional
2799
- # code block is given, it will be passed `io` as an argument, and the IO object
2800
- # will automatically be closed when the block terminates. In this instance,
2801
- # IO.open returns the value of the block.
3200
+ # Creates a new IO object, via IO.new with the given arguments.
2802
3201
  #
2803
- # See IO.new for a description of the `fd`, `mode` and `opt` parameters.
2804
- #
2805
- alias self.open self.for_fd
2806
-
2807
- # <!--
2808
- # rdoc-file=io.c
2809
- # - IO.open(fd, mode="r" [, opt]) -> io
2810
- # - IO.open(fd, mode="r" [, opt]) {|io| block } -> obj
2811
- # -->
2812
- # With no associated block, IO.open is a synonym for IO.new. If the optional
2813
- # code block is given, it will be passed `io` as an argument, and the IO object
2814
- # will automatically be closed when the block terminates. In this instance,
2815
- # IO.open returns the value of the block.
3202
+ # With no block given, returns the IO object.
2816
3203
  #
2817
- # See IO.new for a description of the `fd`, `mode` and `opt` parameters.
3204
+ # With a block given, calls the block with the IO object and returns the block's
3205
+ # value.
2818
3206
  #
2819
- def self.open: [A] (int fd, ?string | int mode, **untyped opt) { (instance) -> A } -> A
2820
- | ...
3207
+ def self.open: (int fd, ?string | int mode, ?path: string?, **untyped opts) -> instance
3208
+ | [A] (int fd, ?string | int mode, ?path: string?, **untyped opts) { (instance) -> A } -> A
2821
3209
 
2822
3210
  def bytes: () { (Integer arg0) -> untyped } -> self
2823
3211
  | () -> ::Enumerator[Integer, self]
@@ -2829,37 +3217,118 @@ class IO < Object
2829
3217
  | () -> ::Enumerator[Integer, self]
2830
3218
 
2831
3219
  # <!-- rdoc-file=io.c -->
2832
- # Executes the block for every line in *ios*, where lines are separated by
2833
- # *sep*. *ios* must be opened for reading or an IOError will be raised.
3220
+ # Calls the block with each remaining line read from the stream; returns `self`.
3221
+ # Does nothing if already at end-of-stream; See [Line IO](rdoc-ref:IO@Line+IO).
2834
3222
  #
2835
- # If no block is given, an enumerator is returned instead.
3223
+ # With no arguments given, reads lines as determined by line separator `$/`:
2836
3224
  #
2837
- # f = File.new("testfile")
2838
- # f.each {|line| puts "#{f.lineno}: #{line}" }
3225
+ # f = File.new('t.txt')
3226
+ # f.each_line {|line| p line }
3227
+ # f.each_line {|line| fail 'Cannot happen' }
3228
+ # f.close
2839
3229
  #
2840
- # *produces:*
3230
+ # Output:
3231
+ #
3232
+ # "First line\n"
3233
+ # "Second line\n"
3234
+ # "\n"
3235
+ # "Fourth line\n"
3236
+ # "Fifth line\n"
3237
+ #
3238
+ # With only string argument `sep` given, reads lines as determined by line
3239
+ # separator `sep`; see [Line Separator](rdoc-ref:IO@Line+Separator):
3240
+ #
3241
+ # f = File.new('t.txt')
3242
+ # f.each_line('li') {|line| p line }
3243
+ # f.close
3244
+ #
3245
+ # Output:
2841
3246
  #
2842
- # 1: This is line one
2843
- # 2: This is line two
2844
- # 3: This is line three
2845
- # 4: And so on...
3247
+ # "First li"
3248
+ # "ne\nSecond li"
3249
+ # "ne\n\nFourth li"
3250
+ # "ne\nFifth li"
3251
+ # "ne\n"
2846
3252
  #
2847
- # See IO.readlines for details about getline_args.
3253
+ # The two special values for `sep` are honored:
3254
+ #
3255
+ # f = File.new('t.txt')
3256
+ # # Get all into one string.
3257
+ # f.each_line(nil) {|line| p line }
3258
+ # f.close
3259
+ #
3260
+ # Output:
3261
+ #
3262
+ # "First line\nSecond line\n\nFourth line\nFifth line\n"
3263
+ #
3264
+ # f.rewind
3265
+ # # Get paragraphs (up to two line separators).
3266
+ # f.each_line('') {|line| p line }
3267
+ #
3268
+ # Output:
3269
+ #
3270
+ # "First line\nSecond line\n\n"
3271
+ # "Fourth line\nFifth line\n"
3272
+ #
3273
+ # With only integer argument `limit` given, limits the number of bytes in each
3274
+ # line; see [Line Limit](rdoc-ref:IO@Line+Limit):
3275
+ #
3276
+ # f = File.new('t.txt')
3277
+ # f.each_line(8) {|line| p line }
3278
+ # f.close
3279
+ #
3280
+ # Output:
3281
+ #
3282
+ # "First li"
3283
+ # "ne\n"
3284
+ # "Second l"
3285
+ # "ine\n"
3286
+ # "\n"
3287
+ # "Fourth l"
3288
+ # "ine\n"
3289
+ # "Fifth li"
3290
+ # "ne\n"
3291
+ #
3292
+ # With arguments `sep` and `limit` given, combines the two behaviors:
3293
+ #
3294
+ # * Calls with the next line as determined by line separator `sep`.
3295
+ # * But returns no more bytes than are allowed by the limit.
3296
+ #
3297
+ #
3298
+ # Optional keyword argument `chomp` specifies whether line separators are to be
3299
+ # omitted:
3300
+ #
3301
+ # f = File.new('t.txt')
3302
+ # f.each_line(chomp: true) {|line| p line }
3303
+ # f.close
3304
+ #
3305
+ # Output:
3306
+ #
3307
+ # "First line"
3308
+ # "Second line"
3309
+ # ""
3310
+ # "Fourth line"
3311
+ # "Fifth line"
3312
+ #
3313
+ # Returns an Enumerator if no block is given.
3314
+ #
3315
+ # IO#each is an alias for IO#each_line.
2848
3316
  #
2849
3317
  def each_line: (?String sep, ?Integer limit) { (String arg0) -> untyped } -> self
2850
3318
  | (?String sep, ?Integer limit) -> ::Enumerator[String, self]
2851
3319
 
2852
3320
  # <!-- rdoc-file=io.c -->
2853
3321
  # Returns `true` if the stream is positioned at its end, `false` otherwise; see
2854
- # [Position](#class-IO-label-Position):
3322
+ # [Position](rdoc-ref:IO@Position):
2855
3323
  #
2856
3324
  # f = File.open('t.txt')
2857
3325
  # f.eof # => false
2858
3326
  # f.seek(0, :END) # => 0
2859
3327
  # f.eof # => true
3328
+ # f.close
2860
3329
  #
2861
3330
  # Raises an exception unless the stream is opened for reading; see
2862
- # [Mode](#class-IO-label-Mode).
3331
+ # [Mode](rdoc-ref:File@Access+Modes).
2863
3332
  #
2864
3333
  # If `self` is a stream such as pipe or socket, this method blocks until the
2865
3334
  # other end sends some data or closes it:
@@ -2879,7 +3348,7 @@ class IO < Object
2879
3348
  # not behave as you intend with IO#eof?, unless you call IO#rewind first (which
2880
3349
  # is not available for some streams).
2881
3350
  #
2882
- # I#eof? is an alias for IO#eof.
3351
+ # IO#eof? is an alias for IO#eof.
2883
3352
  #
2884
3353
  def eof?: () -> bool
2885
3354
 
@@ -2893,6 +3362,7 @@ class IO < Object
2893
3362
  # $stdout.fileno # => 1
2894
3363
  # $stderr.fileno # => 2
2895
3364
  # File.open('t.txt').fileno # => 10
3365
+ # f.close
2896
3366
  #
2897
3367
  # IO#to_i is an alias for IO#fileno.
2898
3368
  #