rbs 3.3.2 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/comments.yml +2 -5
  3. data/.github/workflows/ruby.yml +7 -8
  4. data/.github/workflows/typecheck.yml +37 -0
  5. data/CHANGELOG.md +65 -0
  6. data/Gemfile +1 -1
  7. data/Gemfile.lock +11 -11
  8. data/README.md +1 -0
  9. data/Rakefile +2 -2
  10. data/Steepfile +2 -2
  11. data/core/array.rbs +19 -49
  12. data/core/basic_object.rbs +2 -2
  13. data/core/comparable.rbs +17 -8
  14. data/core/complex.rbs +82 -43
  15. data/core/data.rbs +2 -4
  16. data/core/dir.rbs +635 -295
  17. data/core/enumerable.rbs +11 -18
  18. data/core/enumerator.rbs +37 -31
  19. data/core/errors.rbs +4 -0
  20. data/core/false_class.rbs +34 -15
  21. data/core/fiber.rbs +23 -0
  22. data/core/file.rbs +329 -120
  23. data/core/float.rbs +17 -32
  24. data/core/gc.rbs +17 -11
  25. data/core/hash.rbs +22 -44
  26. data/core/integer.rbs +82 -113
  27. data/core/io/buffer.rbs +90 -47
  28. data/core/io.rbs +54 -121
  29. data/core/kernel.rbs +442 -489
  30. data/core/match_data.rbs +55 -56
  31. data/core/module.rbs +45 -1
  32. data/core/nil_class.rbs +98 -35
  33. data/core/numeric.rbs +22 -32
  34. data/core/object_space/weak_key_map.rbs +102 -0
  35. data/core/process.rbs +1242 -655
  36. data/core/ractor.rbs +139 -120
  37. data/core/range.rbs +100 -4
  38. data/core/rational.rbs +0 -4
  39. data/core/rbs/unnamed/argf.rbs +16 -8
  40. data/core/rbs/unnamed/env_class.rbs +0 -24
  41. data/core/refinement.rbs +8 -0
  42. data/core/regexp.rbs +1149 -598
  43. data/core/ruby_vm.rbs +126 -12
  44. data/core/rubygems/platform.rbs +9 -0
  45. data/core/rubygems/rubygems.rbs +1 -1
  46. data/core/rubygems/version.rbs +5 -1
  47. data/core/set.rbs +20 -22
  48. data/core/signal.rbs +4 -4
  49. data/core/string.rbs +283 -230
  50. data/core/string_io.rbs +2 -14
  51. data/core/struct.rbs +404 -24
  52. data/core/symbol.rbs +1 -19
  53. data/core/thread.rbs +29 -12
  54. data/core/time.rbs +227 -104
  55. data/core/trace_point.rbs +2 -5
  56. data/core/true_class.rbs +54 -21
  57. data/core/warning.rbs +14 -11
  58. data/docs/data_and_struct.md +29 -0
  59. data/docs/gem.md +58 -0
  60. data/docs/syntax.md +3 -5
  61. data/docs/tools.md +1 -0
  62. data/ext/rbs_extension/lexer.c +643 -559
  63. data/ext/rbs_extension/lexer.re +5 -1
  64. data/ext/rbs_extension/parser.c +12 -3
  65. data/ext/rbs_extension/unescape.c +7 -47
  66. data/lib/rbs/cli/diff.rb +4 -1
  67. data/lib/rbs/cli/validate.rb +280 -0
  68. data/lib/rbs/cli.rb +2 -194
  69. data/lib/rbs/collection/config.rb +5 -6
  70. data/lib/rbs/collection/sources/git.rb +1 -1
  71. data/lib/rbs/collection.rb +1 -0
  72. data/lib/rbs/diff.rb +7 -4
  73. data/lib/rbs/errors.rb +11 -0
  74. data/lib/rbs/test/errors.rb +10 -2
  75. data/lib/rbs/test/guaranteed.rb +2 -3
  76. data/lib/rbs/test/type_check.rb +15 -10
  77. data/lib/rbs/test.rb +3 -3
  78. data/lib/rbs/types.rb +29 -0
  79. data/lib/rbs/unit_test/convertibles.rb +176 -0
  80. data/lib/rbs/unit_test/spy.rb +136 -0
  81. data/lib/rbs/unit_test/type_assertions.rb +341 -0
  82. data/lib/rbs/unit_test/with_aliases.rb +143 -0
  83. data/lib/rbs/unit_test.rb +6 -0
  84. data/lib/rbs/version.rb +1 -1
  85. data/sig/cli/validate.rbs +43 -0
  86. data/sig/diff.rbs +3 -1
  87. data/sig/errors.rbs +8 -0
  88. data/sig/rbs.rbs +1 -1
  89. data/sig/test/errors.rbs +52 -0
  90. data/sig/test/guranteed.rbs +9 -0
  91. data/sig/test/type_check.rbs +19 -0
  92. data/sig/test.rbs +82 -0
  93. data/sig/types.rbs +6 -1
  94. data/sig/unit_test/convertibles.rbs +154 -0
  95. data/sig/unit_test/spy.rbs +28 -0
  96. data/sig/unit_test/type_assertions.rbs +194 -0
  97. data/sig/unit_test/with_aliases.rbs +136 -0
  98. data/stdlib/base64/0/base64.rbs +307 -45
  99. data/stdlib/bigdecimal/0/big_decimal.rbs +35 -15
  100. data/stdlib/coverage/0/coverage.rbs +2 -2
  101. data/stdlib/csv/0/csv.rbs +25 -55
  102. data/stdlib/date/0/date.rbs +1 -43
  103. data/stdlib/date/0/date_time.rbs +1 -13
  104. data/stdlib/delegate/0/delegator.rbs +186 -0
  105. data/stdlib/delegate/0/kernel.rbs +47 -0
  106. data/stdlib/delegate/0/simple_delegator.rbs +98 -0
  107. data/stdlib/did_you_mean/0/did_you_mean.rbs +1 -1
  108. data/stdlib/erb/0/erb.rbs +2 -2
  109. data/stdlib/fileutils/0/fileutils.rbs +0 -19
  110. data/stdlib/io-console/0/io-console.rbs +12 -1
  111. data/stdlib/ipaddr/0/ipaddr.rbs +2 -1
  112. data/stdlib/json/0/json.rbs +320 -81
  113. data/stdlib/logger/0/logger.rbs +9 -5
  114. data/stdlib/minitest/0/minitest/test/lifecycle_hooks.rbs +6 -6
  115. data/stdlib/monitor/0/monitor.rbs +78 -0
  116. data/stdlib/net-http/0/net-http.rbs +1880 -543
  117. data/stdlib/objspace/0/objspace.rbs +19 -13
  118. data/stdlib/openssl/0/openssl.rbs +508 -127
  119. data/stdlib/optparse/0/optparse.rbs +25 -11
  120. data/stdlib/pathname/0/pathname.rbs +1 -1
  121. data/stdlib/pp/0/pp.rbs +2 -5
  122. data/stdlib/prettyprint/0/prettyprint.rbs +2 -2
  123. data/stdlib/pstore/0/pstore.rbs +2 -4
  124. data/stdlib/rdoc/0/comment.rbs +1 -2
  125. data/stdlib/resolv/0/resolv.rbs +4 -2
  126. data/stdlib/socket/0/socket.rbs +2 -2
  127. data/stdlib/socket/0/unix_socket.rbs +2 -2
  128. data/stdlib/strscan/0/string_scanner.rbs +3 -2
  129. data/stdlib/tempfile/0/tempfile.rbs +1 -1
  130. data/stdlib/uri/0/common.rbs +245 -123
  131. metadata +24 -4
  132. data/lib/rbs/test/spy.rb +0 -6
data/core/dir.rbs CHANGED
@@ -1,11 +1,85 @@
1
1
  # <!-- rdoc-file=dir.rb -->
2
- # Objects of class Dir are directory streams representing directories in the
3
- # underlying file system. They provide a variety of ways to list directories and
4
- # their contents. See also File.
2
+ # An object of class Dir represents a directory in the underlying file system.
5
3
  #
6
- # The directory used in these examples contains the two regular files
7
- # (`config.h` and `main.rb`), the parent directory (`..`), and the directory
8
- # itself (`.`).
4
+ # It consists mainly of:
5
+ #
6
+ # * A string *path*, given when the object is created, that specifies a
7
+ # directory in the underlying file system; method #path returns the path.
8
+ # * A collection of string *entry names*, each of which is the name of a
9
+ # directory or file in the underlying file system; the entry names may be
10
+ # retrieved in an [array-like fashion](rdoc-ref:Dir@Dir+As+Array-Like) or in
11
+ # a [stream-like fashion](rdoc-ref:Dir@Dir+As+Stream-Like).
12
+ #
13
+ #
14
+ # ## About the Examples
15
+ #
16
+ # Some examples on this page use this simple file tree:
17
+ #
18
+ # example/
19
+ # ├── config.h
20
+ # ├── lib/
21
+ # │ ├── song/
22
+ # │ │ └── karaoke.rb
23
+ # │ └── song.rb
24
+ # └── main.rb
25
+ #
26
+ # Others use the file tree for the [Ruby project
27
+ # itself](https://github.com/ruby/ruby).
28
+ #
29
+ # ## Dir As Array-Like
30
+ #
31
+ # A Dir object is in some ways array-like:
32
+ #
33
+ # * It has instance methods #children, #each, and #each_child.
34
+ # * It includes [module Enumerable](rdoc-ref:Enumerable@What-27s+Here).
35
+ #
36
+ #
37
+ # ## Dir As Stream-Like
38
+ #
39
+ # A Dir object is in some ways stream-like.
40
+ #
41
+ # The stream is initially open for reading, but may be closed manually (using
42
+ # method #close), and will be closed on block exit if created by Dir.open called
43
+ # with a block. The closed stream may not be further manipulated, and may not be
44
+ # reopened.
45
+ #
46
+ # The stream has a *position*, which is the index of an entry in the directory:
47
+ #
48
+ # * The initial position is zero (before the first entry).
49
+ # * Method #tell (aliased as #pos) returns the position.
50
+ # * Method #pos= sets the position (but ignores a value outside the stream),
51
+ # and returns the position.
52
+ # * Method #seek is like #pos=, but returns `self` (convenient for chaining).
53
+ # * Method #read, if not at end-of-stream, reads the next entry and increments
54
+ # the position; if at end-of-stream, does not increment the position.
55
+ # * Method #rewind sets the position to zero.
56
+ #
57
+ #
58
+ # Examples (using the [simple file tree](rdoc-ref:Dir@About+the+Examples)):
59
+ #
60
+ # dir = Dir.new('example') # => #<Dir:example>
61
+ # dir.pos # => 0
62
+ #
63
+ # dir.read # => "."
64
+ # dir.read # => ".."
65
+ # dir.read # => "config.h"
66
+ # dir.read # => "lib"
67
+ # dir.read # => "main.rb"
68
+ # dir.pos # => 5
69
+ # dir.read # => nil
70
+ # dir.pos # => 5
71
+ #
72
+ # dir.rewind # => #<Dir:example>
73
+ # dir.pos # => 0
74
+ #
75
+ # dir.pos = 3 # => 3
76
+ # dir.pos # => 3
77
+ #
78
+ # dir.seek(4) # => #<Dir:example>
79
+ # dir.pos # => 4
80
+ #
81
+ # dir.close # => nil
82
+ # dir.read # Raises IOError.
9
83
  #
10
84
  # ## What's Here
11
85
  #
@@ -94,358 +168,594 @@ class Dir
94
168
 
95
169
  # <!--
96
170
  # rdoc-file=dir.rb
97
- # - Dir.new( string ) -> aDir
98
- # - Dir.new( string, encoding: enc ) -> aDir
171
+ # - Dir.new(dirpath) -> dir
172
+ # - Dir.new(dirpath, encoding: nil) -> dir
99
173
  # -->
100
- # Returns a new directory object for the named directory.
174
+ # Returns a new Dir object for the directory at `dirpath`:
175
+ #
176
+ # Dir.new('.') # => #<Dir:.>
101
177
  #
102
- # The optional *encoding* keyword argument specifies the encoding of the
103
- # directory. If not specified, the filesystem encoding is used.
178
+ # The value given with optional keyword argument `encoding` specifies the
179
+ # encoding for the directory entry names; if `nil` (the default), the file
180
+ # system's encoding is used:
181
+ #
182
+ # Dir.new('.').read.encoding # => #<Encoding:UTF-8>
183
+ # Dir.new('.', encoding: 'US-ASCII').read.encoding # => #<Encoding:US-ASCII>
104
184
  #
105
185
  def initialize: (path dir, ?encoding: encoding?) -> self
106
186
 
107
187
  # <!--
108
188
  # rdoc-file=dir.rb
109
- # - Dir[ string [, string ...] [, base: path] [, sort: true] ] -> array
189
+ # - Dir[*patterns, base: nil, sort: true] -> array
110
190
  # -->
111
- # Equivalent to calling `Dir.glob([`*string,...*`], 0)`.
191
+ # Calls Dir.glob with argument `patterns` and the values of keyword arguments
192
+ # `base` and `sort`; returns the array of selected entry names.
112
193
  #
113
194
  def self.[]: (*path patterns, ?base: path?, ?sort: bool) -> Array[String]
114
195
 
115
196
  # <!--
116
197
  # rdoc-file=dir.c
117
- # - Dir.chdir( [ string] ) -> 0
118
- # - Dir.chdir( [ string] ) {| path | block } -> anObject
119
- # -->
120
- # Changes the current working directory of the process to the given string. When
121
- # called without an argument, changes the directory to the value of the
122
- # environment variable `HOME`, or `LOGDIR`. SystemCallError (probably
123
- # Errno::ENOENT) if the target directory does not exist.
124
- #
125
- # If a block is given, it is passed the name of the new current directory, and
126
- # the block is executed with that as the current directory. The original working
127
- # directory is restored when the block exits. The return value of `chdir` is the
128
- # value of the block. `chdir` blocks can be nested, but in a multi-threaded
129
- # program an error will be raised if a thread attempts to open a `chdir` block
130
- # while another thread has one open or a call to `chdir` without a block occurs
131
- # inside a block passed to `chdir` (even in the same thread).
132
- #
133
- # Dir.chdir("/var/spool/mail")
134
- # puts Dir.pwd
135
- # Dir.chdir("/tmp") do
136
- # puts Dir.pwd
137
- # Dir.chdir("/usr") do
138
- # puts Dir.pwd
198
+ # - Dir.chdir(new_dirpath) -> 0
199
+ # - Dir.chdir -> 0
200
+ # - Dir.chdir(new_dirpath) {|new_dirpath| ... } -> object
201
+ # - Dir.chdir {|cur_dirpath| ... } -> object
202
+ # -->
203
+ # Changes the current working directory.
204
+ #
205
+ # With argument `new_dirpath` and no block, changes to the given `dirpath`:
206
+ #
207
+ # Dir.pwd # => "/example"
208
+ # Dir.chdir('..') # => 0
209
+ # Dir.pwd # => "/"
210
+ #
211
+ # With no argument and no block:
212
+ #
213
+ # * Changes to the value of environment variable `HOME` if defined.
214
+ # * Otherwise changes to the value of environment variable `LOGDIR` if
215
+ # defined.
216
+ # * Otherwise makes no change.
217
+ #
218
+ #
219
+ # With argument `new_dirpath` and a block, temporarily changes the working
220
+ # directory:
221
+ #
222
+ # * Calls the block with the argument.
223
+ # * Changes to the given directory.
224
+ # * Executes the block
225
+ # * Restores the previous working directory.
226
+ # * Returns the block's return value.
227
+ #
228
+ #
229
+ # Example:
230
+ #
231
+ # Dir.chdir('/var/spool/mail')
232
+ # Dir.pwd # => "/var/spool/mail"
233
+ # Dir.chdir('/tmp') do
234
+ # Dir.pwd # => "/tmp"
235
+ # end
236
+ # Dir.pwd # => "/var/spool/mail"
237
+ #
238
+ # With no argument and a block, calls the block with the current working
239
+ # directory (string) and returns the block's return value.
240
+ #
241
+ # Calls to Dir.chdir with blocks may be nested:
242
+ #
243
+ # Dir.chdir('/var/spool/mail')
244
+ # Dir.pwd # => "/var/spool/mail"
245
+ # Dir.chdir('/tmp') do
246
+ # Dir.pwd # => "/tmp"
247
+ # Dir.chdir('/usr') do
248
+ # Dir.pwd # => "/usr"
139
249
  # end
140
- # puts Dir.pwd
250
+ # Dir.pwd # => "/tmp"
141
251
  # end
142
- # puts Dir.pwd
252
+ # Dir.pwd # => "/var/spool/mail"
143
253
  #
144
- # *produces:*
254
+ # In a multi-threaded program an error is raised if a thread attempts to open a
255
+ # `chdir` block while another thread has one open, or a call to `chdir` without
256
+ # a block occurs inside a block passed to `chdir` (even in the same thread).
145
257
  #
146
- # /var/spool/mail
147
- # /tmp
148
- # /usr
149
- # /tmp
150
- # /var/spool/mail
258
+ # Raises an exception if the target directory does not exist.
151
259
  #
152
260
  def self.chdir: (?path dir) -> 0
153
261
  | [U] (?path dir) { (String dir) -> U } -> U
154
262
 
155
263
  # <!--
156
264
  # rdoc-file=dir.c
157
- # - Dir.children( dirname ) -> array
158
- # - Dir.children( dirname, encoding: enc ) -> array
265
+ # - Dir.children(dirpath) -> array
266
+ # - Dir.children(dirpath, encoding: 'UTF-8') -> array
159
267
  # -->
160
- # Returns an array containing all of the filenames except for "." and ".." in
161
- # the given directory. Will raise a SystemCallError if the named directory
162
- # doesn't exist.
268
+ # Returns an array of the entry names in the directory at `dirpath` except for
269
+ # `'.'` and `'..'`; sets the given encoding onto each returned entry name:
270
+ #
271
+ # Dir.children('/example') # => ["config.h", "lib", "main.rb"]
272
+ # Dir.children('/example').first.encoding
273
+ # # => #<Encoding:UTF-8>
274
+ # Dir.children('/example', encoding: 'US-ASCII').first.encoding
275
+ # # => #<Encoding:US-ASCII>
163
276
  #
164
- # The optional *encoding* keyword argument specifies the encoding of the
165
- # directory. If not specified, the filesystem encoding is used.
277
+ # See [String Encoding](rdoc-ref:encodings.rdoc@String+Encoding).
166
278
  #
167
- # Dir.children("testdir") #=> ["config.h", "main.rb"]
279
+ # Raises an exception if the directory does not exist.
168
280
  #
169
281
  def self.children: (path dirname, ?encoding: encoding?) -> Array[String]
170
282
 
171
283
  # <!--
172
284
  # rdoc-file=dir.c
173
- # - Dir.chroot( string ) -> 0
285
+ # - Dir.chroot(dirpath) -> 0
174
286
  # -->
175
- # Changes this process's idea of the file system root. Only a privileged process
176
- # may make this call. Not available on all platforms. On Unix systems, see
177
- # `chroot(2)` for more information.
287
+ # Changes the root directory of the calling process to that specified in
288
+ # `dirpath`. The new root directory is used for pathnames beginning with `'/'`.
289
+ # The root directory is inherited by all children of the calling process.
290
+ #
291
+ # Only a privileged process may call `chroot`.
292
+ #
293
+ # See [Linux chroot](https://man7.org/linux/man-pages/man2/chroot.2.html).
178
294
  #
179
295
  def self.chroot: (path root) -> 0
180
296
 
181
297
  # <!--
182
298
  # rdoc-file=dir.c
183
- # - Dir.delete( string ) -> 0
184
- # - Dir.rmdir( string ) -> 0
185
- # - Dir.unlink( string ) -> 0
299
+ # - Dir.rmdir(dirpath) -> 0
186
300
  # -->
187
- # Deletes the named directory. Raises a subclass of SystemCallError if the
188
- # directory isn't empty.
301
+ # Removes the directory at `dirpath` from the underlying file system:
302
+ #
303
+ # Dir.rmdir('foo') # => 0
304
+ #
305
+ # Raises an exception if the directory is not empty.
189
306
  #
190
307
  def self.delete: (path dirname) -> 0
191
308
 
192
309
  # <!--
193
310
  # rdoc-file=dir.c
194
- # - Dir.each_child( dirname ) {| filename | block } -> nil
195
- # - Dir.each_child( dirname, encoding: enc ) {| filename | block } -> nil
196
- # - Dir.each_child( dirname ) -> an_enumerator
197
- # - Dir.each_child( dirname, encoding: enc ) -> an_enumerator
311
+ # - Dir.each_child(dirpath) {|entry_name| ... } -> nil
312
+ # - Dir.each_child(dirpath, encoding: 'UTF-8') {|entry_name| ... } -> nil
198
313
  # -->
199
- # Calls the block once for each entry except for "." and ".." in the named
200
- # directory, passing the filename of each entry as a parameter to the block.
201
- #
202
- # If no block is given, an enumerator is returned instead.
203
- #
204
- # Dir.each_child("testdir") {|x| puts "Got #{x}" }
205
- #
206
- # *produces:*
207
- #
208
- # Got config.h
209
- # Got main.rb
314
+ # Like Dir.foreach, except that entries `'.'` and `'..'` are not included.
210
315
  #
211
316
  def self.each_child: (path dirname, ?encoding: encoding?) -> Enumerator[String, nil]
212
317
  | (path dirname, ?encoding: encoding?) { (String filename) -> void } -> nil
213
318
 
214
319
  # <!--
215
320
  # rdoc-file=dir.c
216
- # - Dir.empty?(path_name) -> true or false
321
+ # - Dir.empty?(dirpath) -> true or false
217
322
  # -->
218
- # Returns `true` if the named file is an empty directory, `false` if it is not a
219
- # directory or non-empty.
323
+ # Returns whether `dirpath` specifies an empty directory:
324
+ #
325
+ # dirpath = '/tmp/foo'
326
+ # Dir.mkdir(dirpath)
327
+ # Dir.empty?(dirpath) # => true
328
+ # Dir.empty?('/example') # => false
329
+ # Dir.empty?('/example/main.rb') # => false
330
+ #
331
+ # Raises an exception if `dirpath` does not specify a directory or file in the
332
+ # underlying file system.
220
333
  #
221
334
  def self.empty?: (path path_name) -> bool
222
335
 
223
336
  # <!--
224
337
  # rdoc-file=dir.c
225
- # - Dir.entries( dirname ) -> array
226
- # - Dir.entries( dirname, encoding: enc ) -> array
338
+ # - Dir.entries(dirname, encoding: 'UTF-8') -> array
227
339
  # -->
228
- # Returns an array containing all of the filenames in the given directory. Will
229
- # raise a SystemCallError if the named directory doesn't exist.
340
+ # Returns an array of the entry names in the directory at `dirpath`; sets the
341
+ # given encoding onto each returned entry name:
230
342
  #
231
- # The optional *encoding* keyword argument specifies the encoding of the
232
- # directory. If not specified, the filesystem encoding is used.
343
+ # Dir.entries('/example') # => ["config.h", "lib", "main.rb", "..", "."]
344
+ # Dir.entries('/example').first.encoding
345
+ # # => #<Encoding:UTF-8>
346
+ # Dir.entries('/example', encoding: 'US-ASCII').first.encoding
347
+ # # => #<Encoding:US-ASCII>
233
348
  #
234
- # Dir.entries("testdir") #=> [".", "..", "config.h", "main.rb"]
349
+ # See [String Encoding](rdoc-ref:encodings.rdoc@String+Encoding).
350
+ #
351
+ # Raises an exception if the directory does not exist.
235
352
  #
236
353
  def self.entries: (path dirname, ?encoding: encoding?) -> Array[String]
237
354
 
238
355
  # <!--
239
356
  # rdoc-file=dir.c
240
- # - Dir.exist?(file_name) -> true or false
357
+ # - Dir.exist?(dirpath) -> true or false
241
358
  # -->
242
- # Returns `true` if the named file is a directory, `false` otherwise.
359
+ # Returns whether `dirpath` is a directory in the underlying file system:
360
+ #
361
+ # Dir.exist?('/example') # => true
362
+ # Dir.exist?('/nosuch') # => false
363
+ # Dir.exist?('/example/main.rb') # => false
364
+ #
365
+ # Same as File.directory?.
243
366
  #
244
367
  def self.exist?: (path | io file_name) -> bool
245
368
 
246
369
  # <!--
247
370
  # rdoc-file=dir.c
248
- # - Dir.foreach( dirname ) {| filename | block } -> nil
249
- # - Dir.foreach( dirname, encoding: enc ) {| filename | block } -> nil
250
- # - Dir.foreach( dirname ) -> an_enumerator
251
- # - Dir.foreach( dirname, encoding: enc ) -> an_enumerator
371
+ # - Dir.fchdir(fd) -> 0
372
+ # - Dir.fchdir(fd) { ... } -> object
252
373
  # -->
253
- # Calls the block once for each entry in the named directory, passing the
254
- # filename of each entry as a parameter to the block.
374
+ # Changes the current working directory to the directory specified by the
375
+ # integer file descriptor `fd`.
376
+ #
377
+ # When passing a file descriptor over a UNIX socket or to a child process, using
378
+ # `fchdir` instead of `chdir` avoids the [time-of-check to time-of-use
379
+ # vulnerability](https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use)
380
+ #
381
+ # With no block, changes to the directory given by `fd`:
382
+ #
383
+ # Dir.chdir('/var/spool/mail')
384
+ # Dir.pwd # => "/var/spool/mail"
385
+ # dir = Dir.new('/usr')
386
+ # fd = dir.fileno
387
+ # Dir.fchdir(fd) do
388
+ # Dir.pwd # => "/usr"
389
+ # end
390
+ # Dir.pwd # => "/var/spool/mail"
391
+ #
392
+ # With a block, temporarily changes the working directory:
393
+ #
394
+ # * Calls the block with the argument.
395
+ # * Changes to the given directory.
396
+ # * Executes the block
397
+ # * Restores the previous working directory.
398
+ # * Returns the block's return value.
399
+ #
400
+ #
401
+ # Example:
402
+ #
403
+ # Dir.chdir('/var/spool/mail')
404
+ # Dir.pwd # => "/var/spool/mail"
405
+ # Dir.chdir('/tmp') do
406
+ # Dir.pwd # => "/tmp"
407
+ # end
408
+ # Dir.pwd # => "/var/spool/mail"
409
+ #
410
+ # This method uses the
411
+ # [fchdir()](https://www.man7.org/linux/man-pages/man3/fchdir.3p.html) function
412
+ # defined by POSIX 2008; the method is not implemented on non-POSIX platforms
413
+ # (raises NotImplementedError).
414
+ #
415
+ # Raises an exception if the file descriptor is not valid.
416
+ #
417
+ # In a multi-threaded program an error is raised if a thread attempts to open a
418
+ # `chdir` block while another thread has one open, or a call to `chdir` without
419
+ # a block occurs inside a block passed to `chdir` (even in the same thread).
420
+ #
421
+ def self.fchdir: (int) -> Integer
422
+ | [T] (int) { () -> T } -> T
423
+
424
+ # <!--
425
+ # rdoc-file=dir.c
426
+ # - Dir.foreach(dirpath, encoding: 'UTF-8') {|entry_name| ... } -> nil
427
+ # -->
428
+ # Calls the block with each entry name in the directory at `dirpath`; sets the
429
+ # given encoding onto each passed `entry_name`:
430
+ #
431
+ # Dir.foreach('/example') {|entry_name| p entry_name }
432
+ #
433
+ # Output:
434
+ #
435
+ # "config.h"
436
+ # "lib"
437
+ # "main.rb"
438
+ # ".."
439
+ # "."
255
440
  #
256
- # If no block is given, an enumerator is returned instead.
441
+ # Encoding:
257
442
  #
258
- # Dir.foreach("testdir") {|x| puts "Got #{x}" }
443
+ # Dir.foreach('/example') {|entry_name| p entry_name.encoding; break }
444
+ # Dir.foreach('/example', encoding: 'US-ASCII') {|entry_name| p entry_name.encoding; break }
259
445
  #
260
- # *produces:*
446
+ # Output:
261
447
  #
262
- # Got .
263
- # Got ..
264
- # Got config.h
265
- # Got main.rb
448
+ # #<Encoding:UTF-8>
449
+ # #<Encoding:US-ASCII>
450
+ #
451
+ # See [String Encoding](rdoc-ref:encodings.rdoc@String+Encoding).
452
+ #
453
+ # Returns an enumerator if no block is given.
266
454
  #
267
455
  alias self.foreach self.each_child
268
456
 
269
457
  # <!--
270
458
  # rdoc-file=dir.c
271
- # - Dir.getwd -> string
459
+ # - Dir.for_fd(fd) -> dir
460
+ # -->
461
+ # Returns a new Dir object representing the directory specified by the given
462
+ # integer directory file descriptor `fd`:
463
+ #
464
+ # d0 = Dir.new('..')
465
+ # d1 = Dir.for_fd(d0.fileno)
466
+ #
467
+ # Note that the returned `d1` does not have an associated path:
468
+ #
469
+ # d0.path # => '..'
470
+ # d1.path # => nil
471
+ #
472
+ # This method uses the
473
+ # [fdopendir()](https://www.man7.org/linux/man-pages/man3/fdopendir.3p.html)
474
+ # function defined by POSIX 2008; the method is not implemented on non-POSIX
475
+ # platforms (raises NotImplementedError).
476
+ #
477
+ def self.for_fd: (int) -> Dir
478
+
479
+ # <!--
480
+ # rdoc-file=dir.c
272
481
  # - Dir.pwd -> string
273
482
  # -->
274
- # Returns the path to the current working directory of this process as a string.
483
+ # Returns the path to the current working directory:
275
484
  #
276
- # Dir.chdir("/tmp") #=> 0
277
- # Dir.getwd #=> "/tmp"
278
- # Dir.pwd #=> "/tmp"
485
+ # Dir.chdir("/tmp") # => 0
486
+ # Dir.pwd # => "/tmp"
279
487
  #
280
488
  def self.getwd: () -> String
281
489
 
282
490
  # <!--
283
491
  # rdoc-file=dir.rb
284
- # - Dir.glob( pattern, [flags], [base: path] [, sort: true] ) -> array
285
- # - Dir.glob( pattern, [flags], [base: path] [, sort: true] ) { |filename| block } -> nil
492
+ # - Dir.glob(*patterns, flags: 0, base: nil, sort: true) -> array
493
+ # - Dir.glob(*patterns, flags: 0, base: nil, sort: true) {|entry_name| ... } -> nil
286
494
  # -->
287
- # Expands `pattern`, which is a pattern string or an Array of pattern strings,
288
- # and returns an array containing the matching filenames. If a block is given,
289
- # calls the block once for each matching filename, passing the filename as a
290
- # parameter to the block.
495
+ # Forms an array *entry_names* of the entry names selected by the arguments.
291
496
  #
292
- # The optional `base` keyword argument specifies the base directory for
293
- # interpreting relative pathnames instead of the current working directory. As
294
- # the results are not prefixed with the base directory name in this case, you
295
- # will need to prepend the base directory name if you want real paths.
497
+ # Argument `patterns` is a string pattern or an array of string patterns; note
498
+ # that these are not regexps; see below.
296
499
  #
297
- # The results which matched single wildcard or character set are sorted in
298
- # binary ascending order, unless `false` is given as the optional `sort` keyword
299
- # argument. The order of an Array of pattern strings and braces are preserved.
500
+ # Notes for the following examples:
300
501
  #
301
- # Note that the pattern is not a regexp, it's closer to a shell glob. See
302
- # File::fnmatch for the meaning of the `flags` parameter. Case sensitivity
303
- # depends on your system (`File::FNM_CASEFOLD` is ignored).
502
+ # * `'*'` is the pattern that matches any entry name except those that begin
503
+ # with `'.'`.
504
+ # * We use method Array#take to shorten returned arrays that otherwise would
505
+ # be very large.
304
506
  #
305
- # `*`
306
- # : Matches any file. Can be restricted by other values in the glob.
307
- # Equivalent to `/.*/mx` in regexp.
308
507
  #
309
- # `*`
310
- # : Matches all files
311
- # `c*`
312
- # : Matches all files beginning with `c`
313
- # `*c`
314
- # : Matches all files ending with `c`
315
- # `*c*`
316
- # : Match all files that have `c` in them (including at the beginning or
317
- # end).
508
+ # With no block, returns array *entry_names*; example (using the [simple file
509
+ # tree](rdoc-ref:Dir@About+the+Examples)):
318
510
  #
511
+ # Dir.glob('*') # => ["config.h", "lib", "main.rb"]
319
512
  #
320
- # Note, this will not match Unix-like hidden files (dotfiles). In order to
321
- # include those in the match results, you must use the File::FNM_DOTMATCH
322
- # flag or something like `"{*,.*}"`.
513
+ # With a block, calls the block with each of the *entry_names* and returns
514
+ # `nil`:
323
515
  #
324
- # `**`
325
- # : Matches directories recursively if followed by `/`. If this path segment
326
- # contains any other characters, it is the same as the usual `*`.
516
+ # Dir.glob('*') {|entry_name| puts entry_name } # => nil
327
517
  #
328
- # `?`
329
- # : Matches any one character. Equivalent to `/.{1}/` in regexp.
518
+ # Output:
330
519
  #
331
- # `[set]`
332
- # : Matches any one character in `set`. Behaves exactly like character sets
333
- # in Regexp, including set negation (`[^a-z]`).
520
+ # config.h
521
+ # lib
522
+ # main.rb
334
523
  #
335
- # `{p,q}`
336
- # : Matches either literal `p` or literal `q`. Equivalent to pattern
337
- # alternation in regexp.
524
+ # If optional keyword argument `flags` is given, the value modifies the
525
+ # matching; see below.
338
526
  #
339
- # Matching literals may be more than one character in length. More than two
340
- # literals may be specified.
527
+ # If optional keyword argument `base` is given, its value specifies the base
528
+ # directory. Each pattern string specifies entries relative to the base
529
+ # directory; the default is `'.'`. The base directory is not prepended to the
530
+ # entry names in the result:
341
531
  #
342
- # `\`
343
- # : Escapes the next metacharacter.
532
+ # Dir.glob(pattern, base: 'lib').take(5)
533
+ # # => ["abbrev.gemspec", "abbrev.rb", "base64.gemspec", "base64.rb", "benchmark.gemspec"]
534
+ # Dir.glob(pattern, base: 'lib/irb').take(5)
535
+ # # => ["cmd", "color.rb", "color_printer.rb", "completion.rb", "context.rb"]
344
536
  #
345
- # Note that this means you cannot use backslash on windows as part of a
346
- # glob, i.e. `Dir["c:\\foo*"]` will not work, use `Dir["c:/foo*"]` instead.
537
+ # If optional keyword `sort` is given, its value specifies whether the array is
538
+ # to be sorted; the default is `true`. Passing value `false` with that keyword
539
+ # disables sorting (though the underlying file system may already have sorted
540
+ # the array).
347
541
  #
542
+ # **Patterns**
348
543
  #
349
- # Examples:
544
+ # Each pattern string is expanded according to certain metacharacters; examples
545
+ # below use the [Ruby file tree](rdoc-ref:Dir@About+the+Examples):
546
+ #
547
+ # * `'*'`: Matches any substring in an entry name, similar in meaning to
548
+ # regexp `/.*/mx`; may be restricted by other values in the pattern strings:
549
+ #
550
+ # * `'*'` matches all entry names:
551
+ #
552
+ # Dir.glob('*').take(3) # => ["BSDL", "CONTRIBUTING.md", "COPYING"]
553
+ #
554
+ # * `'c*'` matches entry names beginning with `'c'`:
555
+ #
556
+ # Dir.glob('c*').take(3) # => ["CONTRIBUTING.md", "COPYING", "COPYING.ja"]
557
+ #
558
+ # * `'*c'` matches entry names ending with `'c'`:
559
+ #
560
+ # Dir.glob('*c').take(3) # => ["addr2line.c", "array.c", "ast.c"]
561
+ #
562
+ # * `'*c*'` matches entry names that contain `'c'`, even at the beginning
563
+ # or end:
564
+ #
565
+ # Dir.glob('*c*').take(3) # => ["CONTRIBUTING.md", "COPYING", "COPYING.ja"]
566
+ #
567
+ #
568
+ # Does not match Unix-like hidden entry names ("dot files"). To include
569
+ # those in the matched entry names, use flag IO::FNM_DOTMATCH or something
570
+ # like `'{*,.*}'`.
571
+ #
572
+ # * `'**'`: Matches entry names recursively if followed by the slash
573
+ # character `'/'`:
574
+ #
575
+ # Dir.glob('**/').take(3) # => ["basictest/", "benchmark/", "benchmark/gc/"]
576
+ #
577
+ # If the string pattern contains other characters or is not followed by a
578
+ # slash character, it is equivalent to `'*'`.
579
+ #
580
+ # * `'?'` Matches any single character; similar in meaning to regexp `/./`:
581
+ #
582
+ # Dir.glob('io.?') # => ["io.c"]
583
+ #
584
+ # * `'[*set*]'`: Matches any one character in the string *set*; behaves like a
585
+ # [Regexp character class](rdoc-ref:Regexp@Character+Classes), including set
586
+ # negation (`'[^a-z]'`):
587
+ #
588
+ # Dir.glob('*.[a-z][a-z]').take(3)
589
+ # # => ["CONTRIBUTING.md", "COPYING.ja", "KNOWNBUGS.rb"]
590
+ #
591
+ # * `'{*abc*,*xyz*}'`: Matches either string *abc* or string *xyz*; behaves
592
+ # like [Regexp alternation](rdoc-ref:Regexp@Alternation):
593
+ #
594
+ # Dir.glob('{LEGAL,BSDL}') # => ["LEGAL", "BSDL"]
595
+ #
596
+ # More than two alternatives may be given.
597
+ #
598
+ # * `\`: Escapes the following metacharacter.
599
+ #
600
+ # Note that on Windows, the backslash character may not be used in a string
601
+ # pattern: `Dir['c:\\foo*']` will not work, use `Dir['c:/foo*']` instead.
602
+ #
603
+ #
604
+ # More examples (using the [simple file tree](rdoc-ref:Dir@About+the+Examples)):
605
+ #
606
+ # # We're in the example directory.
607
+ # File.basename(Dir.pwd) # => "example"
608
+ # Dir.glob('config.?') # => ["config.h"]
609
+ # Dir.glob('*.[a-z][a-z]') # => ["main.rb"]
610
+ # Dir.glob('*.[^r]*') # => ["config.h"]
611
+ # Dir.glob('*.{rb,h}') # => ["main.rb", "config.h"]
612
+ # Dir.glob('*') # => ["config.h", "lib", "main.rb"]
613
+ # Dir.glob('*', File::FNM_DOTMATCH) # => [".", "config.h", "lib", "main.rb"]
614
+ # Dir.glob(["*.rb", "*.h"]) # => ["main.rb", "config.h"]
350
615
  #
351
- # Dir["config.?"] #=> ["config.h"]
352
- # Dir.glob("config.?") #=> ["config.h"]
353
- # Dir.glob("*.[a-z][a-z]") #=> ["main.rb"]
354
- # Dir.glob("*.[^r]*") #=> ["config.h"]
355
- # Dir.glob("*.{rb,h}") #=> ["main.rb", "config.h"]
356
- # Dir.glob("*") #=> ["config.h", "main.rb"]
357
- # Dir.glob("*", File::FNM_DOTMATCH) #=> [".", "config.h", "main.rb"]
358
- # Dir.glob(["*.rb", "*.h"]) #=> ["main.rb", "config.h"]
616
+ # Dir.glob('**/*.rb')
617
+ # => ["lib/song/karaoke.rb", "lib/song.rb", "main.rb"]
359
618
  #
360
- # Dir.glob("**/*.rb") #=> ["main.rb",
361
- # # "lib/song.rb",
362
- # # "lib/song/karaoke.rb"]
619
+ # Dir.glob('**/*.rb', base: 'lib') # => ["song/karaoke.rb", "song.rb"]
363
620
  #
364
- # Dir.glob("**/*.rb", base: "lib") #=> ["song.rb",
365
- # # "song/karaoke.rb"]
621
+ # Dir.glob('**/lib') # => ["lib"]
366
622
  #
367
- # Dir.glob("**/lib") #=> ["lib"]
623
+ # Dir.glob('**/lib/**/*.rb') # => ["lib/song/karaoke.rb", "lib/song.rb"]
368
624
  #
369
- # Dir.glob("**/lib/**/*.rb") #=> ["lib/song.rb",
370
- # # "lib/song/karaoke.rb"]
625
+ # Dir.glob('**/lib/*.rb') # => ["lib/song.rb"]
371
626
  #
372
- # Dir.glob("**/lib/*.rb") #=> ["lib/song.rb"]
627
+ # **Flags**
628
+ #
629
+ # If optional keyword argument `flags` is given (the default is zero -- no
630
+ # flags), its value should be the bitwise OR of one or more of the constants
631
+ # defined in module File::Constants.
632
+ #
633
+ # Example:
634
+ #
635
+ # flags = File::FNM_EXTGLOB | File::FNM_DOTMATCH
636
+ #
637
+ # Specifying flags can extend, restrict, or otherwise modify the matching.
638
+ #
639
+ # The flags for this method (other constants in File::Constants do not apply):
640
+ #
641
+ # * File::FNM_DOTMATCH: specifies that entry names beginning with `'.'` should
642
+ # be considered for matching:
643
+ #
644
+ # Dir.glob('*').take(5)
645
+ # # => ["BSDL", "CONTRIBUTING.md", "COPYING", "COPYING.ja", "GPL"]
646
+ # Dir.glob('*', flags: File::FNM_DOTMATCH).take(5)
647
+ # # => [".", ".appveyor.yml", ".cirrus.yml", ".dir-locals.el", ".document"]
648
+ #
649
+ # * File::FNM_EXTGLOB: enables the pattern extension `'{*a*,*b*}'`, which
650
+ # matches pattern *a* and pattern *b*; behaves like a [regexp
651
+ # union](rdoc-ref:Regexp.union) (e.g., `'(?:*a*|*b*)'`):
652
+ #
653
+ # pattern = '{LEGAL,BSDL}'
654
+ # Dir.glob(pattern) # => ["LEGAL", "BSDL"]
655
+ #
656
+ # * File::FNM_NOESCAPE: specifies that escaping with the backslash character
657
+ # `'\'` is disabled; the character is not an escape character.
658
+ #
659
+ # * File::FNM_PATHNAME: specifies that metacharacters `'*'` and `'?'` do not
660
+ # match directory separators.
661
+ #
662
+ # * File::FNM_SHORTNAME: specifies that patterns may match short names if they
663
+ # exist; Windows only.
373
664
  #
374
665
  def self.glob: (array[path] | path pattern, ?int flags, ?base: path?, ?sort: bool) -> Array[String]
375
666
  | (array[path] | path pattern, ?int flags, ?base: path?, ?sort: bool) { (String pathname) -> void } -> nil
376
667
 
377
668
  # <!--
378
669
  # rdoc-file=dir.c
379
- # - Dir.home() -> "/home/me"
380
- # - Dir.home("root") -> "/root"
670
+ # - Dir.home(user_name = nil) -> dirpath
381
671
  # -->
382
- # Returns the home directory of the current user or the named user if given.
672
+ # Retruns the home directory path of the user specified with `user_name` if it
673
+ # is not `nil`, or the current login user:
674
+ #
675
+ # Dir.home # => "/home/me"
676
+ # Dir.home('root') # => "/root"
677
+ #
678
+ # Raises ArgumentError if `user_name` is not a user name.
383
679
  #
384
680
  def self.home: (?string? user) -> String
385
681
 
386
682
  # <!--
387
683
  # rdoc-file=dir.c
388
- # - Dir.mkdir( string [, integer] ) -> 0
684
+ # - Dir.mkdir(dirpath, permissions = 0775) -> 0
389
685
  # -->
390
- # Makes a new directory named by *string*, with permissions specified by the
391
- # optional parameter *anInteger*. The permissions may be modified by the value
392
- # of File::umask, and are ignored on NT. Raises a SystemCallError if the
393
- # directory cannot be created. See also the discussion of permissions in the
394
- # class documentation for File.
686
+ # Creates a directory in the underlying file system at `dirpath` with the given
687
+ # `permissions`; returns zero:
688
+ #
689
+ # Dir.mkdir('foo')
690
+ # File.stat(Dir.new('foo')).mode.to_s(8)[1..4] # => "0755"
691
+ # Dir.mkdir('bar', 0644)
692
+ # File.stat(Dir.new('bar')).mode.to_s(8)[1..4] # => "0644"
395
693
  #
396
- # Dir.mkdir(File.join(Dir.home, ".foo"), 0700) #=> 0
694
+ # See [File Permissions](rdoc-ref:File@File+Permissions). Note that argument
695
+ # `permissions` is ignored on Windows.
397
696
  #
398
697
  def self.mkdir: (path dirname, ?int permissions) -> 0
399
698
 
400
699
  # <!--
401
700
  # rdoc-file=dir.rb
402
- # - Dir.open( string ) -> aDir
403
- # - Dir.open( string, encoding: enc ) -> aDir
404
- # - Dir.open( string ) {| aDir | block } -> anObject
405
- # - Dir.open( string, encoding: enc ) {| aDir | block } -> anObject
701
+ # - Dir.open(dirpath) -> dir
702
+ # - Dir.open(dirpath, encoding: nil) -> dir
703
+ # - Dir.open(dirpath) {|dir| ... } -> object
704
+ # - Dir.open(dirpath, encoding: nil) {|dir| ... } -> object
406
705
  # -->
407
- # The optional *encoding* keyword argument specifies the encoding of the
408
- # directory. If not specified, the filesystem encoding is used.
706
+ # Creates a new Dir object *dir* for the directory at `dirpath`.
707
+ #
708
+ # With no block, the method equivalent to Dir.new(dirpath, encoding):
709
+ #
710
+ # Dir.open('.') # => #<Dir:.>
711
+ #
712
+ # With a block given, the block is called with the created *dir*; on block exit
713
+ # *dir* is closed and the block's value is returned:
714
+ #
715
+ # Dir.open('.') {|dir| dir.inspect } # => "#<Dir:.>"
409
716
  #
410
- # With no block, `open` is a synonym for Dir::new. If a block is present, it is
411
- # passed *aDir* as a parameter. The directory is closed at the end of the block,
412
- # and Dir::open returns the value of the block.
717
+ # The value given with optional keyword argument `encoding` specifies the
718
+ # encoding for the directory entry names; if `nil` (the default), the file
719
+ # system's encoding is used:
720
+ #
721
+ # Dir.open('.').read.encoding # => #<Encoding:UTF-8>
722
+ # Dir.open('.', encoding: 'US-ASCII').read.encoding # => #<Encoding:US-ASCII>
413
723
  #
414
724
  def self.open: (path dirname, ?encoding: encoding?) -> instance
415
725
  | [U] (path dirname, ?encoding: encoding?) { (instance) -> U } -> U
416
726
 
417
727
  # <!--
418
728
  # rdoc-file=dir.c
419
- # - Dir.getwd -> string
420
729
  # - Dir.pwd -> string
421
730
  # -->
422
- # Returns the path to the current working directory of this process as a string.
731
+ # Returns the path to the current working directory:
423
732
  #
424
- # Dir.chdir("/tmp") #=> 0
425
- # Dir.getwd #=> "/tmp"
426
- # Dir.pwd #=> "/tmp"
733
+ # Dir.chdir("/tmp") # => 0
734
+ # Dir.pwd # => "/tmp"
427
735
  #
428
736
  alias self.pwd self.getwd
429
737
 
430
738
  # <!--
431
739
  # rdoc-file=dir.c
432
- # - Dir.delete( string ) -> 0
433
- # - Dir.rmdir( string ) -> 0
434
- # - Dir.unlink( string ) -> 0
740
+ # - Dir.rmdir(dirpath) -> 0
435
741
  # -->
436
- # Deletes the named directory. Raises a subclass of SystemCallError if the
437
- # directory isn't empty.
742
+ # Removes the directory at `dirpath` from the underlying file system:
743
+ #
744
+ # Dir.rmdir('foo') # => 0
745
+ #
746
+ # Raises an exception if the directory is not empty.
438
747
  #
439
748
  alias self.rmdir self.delete
440
749
 
441
750
  # <!--
442
751
  # rdoc-file=dir.c
443
- # - Dir.delete( string ) -> 0
444
- # - Dir.rmdir( string ) -> 0
445
- # - Dir.unlink( string ) -> 0
752
+ # - Dir.rmdir(dirpath) -> 0
446
753
  # -->
447
- # Deletes the named directory. Raises a subclass of SystemCallError if the
448
- # directory isn't empty.
754
+ # Removes the directory at `dirpath` from the underlying file system:
755
+ #
756
+ # Dir.rmdir('foo') # => 0
757
+ #
758
+ # Raises an exception if the directory is not empty.
449
759
  #
450
760
  alias self.unlink self.delete
451
761
 
@@ -453,194 +763,224 @@ class Dir
453
763
 
454
764
  # <!--
455
765
  # rdoc-file=dir.c
456
- # - dir.children -> array
766
+ # - chdir -> nil
767
+ # -->
768
+ # Changes the current working directory to the path of `self`:
769
+ #
770
+ # Dir.pwd # => "/"
771
+ # dir = Dir.new('example')
772
+ # dir.chdir
773
+ # Dir.pwd # => "/example"
774
+ #
775
+ def chdir: () -> Integer
776
+ | [T] { () -> T } -> T
777
+
778
+ # <!--
779
+ # rdoc-file=dir.c
780
+ # - children -> array
457
781
  # -->
458
- # Returns an array containing all of the filenames except for "." and ".." in
459
- # this directory.
782
+ # Returns an array of the entry names in `self` except for `'.'` and `'..'`:
460
783
  #
461
- # d = Dir.new("testdir")
462
- # d.children #=> ["config.h", "main.rb"]
784
+ # dir = Dir.new('/example')
785
+ # dir.children # => ["config.h", "lib", "main.rb"]
463
786
  #
464
787
  def children: () -> Array[String]
465
788
 
466
789
  # <!--
467
790
  # rdoc-file=dir.c
468
- # - dir.close -> nil
791
+ # - close -> nil
469
792
  # -->
470
- # Closes the directory stream. Calling this method on closed Dir object is
471
- # ignored since Ruby 2.3.
793
+ # Closes the stream in `self`, if it is open, and returns `nil`; ignored if
794
+ # `self` is already closed:
472
795
  #
473
- # d = Dir.new("testdir")
474
- # d.close #=> nil
796
+ # dir = Dir.new('example')
797
+ # dir.read # => "."
798
+ # dir.close # => nil
799
+ # dir.close # => nil
800
+ # dir.read # Raises IOError.
475
801
  #
476
802
  def close: () -> nil
477
803
 
478
804
  # <!--
479
805
  # rdoc-file=dir.c
480
- # - dir.each { |filename| block } -> dir
481
- # - dir.each -> an_enumerator
806
+ # - each {|entry_name| ... } -> self
482
807
  # -->
483
- # Calls the block once for each entry in this directory, passing the filename of
484
- # each entry as a parameter to the block.
808
+ # Calls the block with each entry name in `self`:
485
809
  #
486
- # If no block is given, an enumerator is returned instead.
810
+ # Dir.new('example').each {|entry_name| p entry_name }
487
811
  #
488
- # d = Dir.new("testdir")
489
- # d.each {|x| puts "Got #{x}" }
812
+ # Output:
490
813
  #
491
- # *produces:*
814
+ # "."
815
+ # ".."
816
+ # "config.h"
817
+ # "lib"
818
+ # "main.rb"
492
819
  #
493
- # Got .
494
- # Got ..
495
- # Got config.h
496
- # Got main.rb
820
+ # With no block given, returns an Enumerator.
497
821
  #
498
822
  def each: () { (String) -> void } -> self
499
823
  | () -> Enumerator[String, self]
500
824
 
501
825
  # <!--
502
826
  # rdoc-file=dir.c
503
- # - dir.each_child {| filename | block } -> dir
504
- # - dir.each_child -> an_enumerator
827
+ # - each_child {|entry_name| ... } -> self
505
828
  # -->
506
- # Calls the block once for each entry except for "." and ".." in this directory,
507
- # passing the filename of each entry as a parameter to the block.
829
+ # Calls the block with each entry name in `self` except `'.'` and `'..'`:
508
830
  #
509
- # If no block is given, an enumerator is returned instead.
831
+ # dir = Dir.new('/example')
832
+ # dir.each_child {|entry_name| p entry_name }
510
833
  #
511
- # d = Dir.new("testdir")
512
- # d.each_child {|x| puts "Got #{x}" }
834
+ # Output:
513
835
  #
514
- # *produces:*
836
+ # "config.h"
837
+ # "lib"
838
+ # "main.rb"
515
839
  #
516
- # Got config.h
517
- # Got main.rb
840
+ # If no block is given, returns an enumerator.
518
841
  #
519
842
  def each_child: () { (String) -> void } -> self
520
843
  | () -> Enumerator[String, self]
521
844
 
522
845
  # <!--
523
846
  # rdoc-file=dir.c
524
- # - dir.fileno -> integer
847
+ # - fileno -> integer
525
848
  # -->
526
849
  # Returns the file descriptor used in *dir*.
527
850
  #
528
- # d = Dir.new("..")
529
- # d.fileno #=> 8
851
+ # d = Dir.new('..')
852
+ # d.fileno # => 8
530
853
  #
531
- # This method uses dirfd() function defined by POSIX 2008. NotImplementedError
532
- # is raised on other platforms, such as Windows, which doesn't provide the
533
- # function.
854
+ # This method uses the
855
+ # [dirfd()](https://www.man7.org/linux/man-pages/man3/dirfd.3.html) function
856
+ # defined by POSIX 2008; the method is not implemented on non-POSIX platforms
857
+ # (raises NotImplementedError).
534
858
  #
535
859
  def fileno: () -> Integer
536
860
 
537
861
  # <!--
538
862
  # rdoc-file=dir.c
539
- # - dir.inspect -> string
863
+ # - inspect -> string
540
864
  # -->
541
- # Return a string describing this Dir object.
865
+ # Returns a string description of `self`:
866
+ #
867
+ # Dir.new('example').inspect # => "#<Dir:example>"
542
868
  #
543
869
  def inspect: () -> String
544
870
 
545
871
  # <!--
546
872
  # rdoc-file=dir.c
547
- # - dir.path -> string or nil
548
- # - dir.to_path -> string or nil
873
+ # - path -> string or nil
549
874
  # -->
550
- # Returns the path parameter passed to *dir*'s constructor.
875
+ # Returns the `dirpath` string that was used to create `self` (or `nil` if
876
+ # created by method Dir.for_fd):
551
877
  #
552
- # d = Dir.new("..")
553
- # d.path #=> ".."
878
+ # Dir.new('example').path # => "example"
554
879
  #
555
880
  def path: () -> String?
556
881
 
557
882
  # <!-- rdoc-file=dir.c -->
558
- # Returns the current position in *dir*. See also Dir#seek.
883
+ # Returns the current position of `self`; see [Dir As
884
+ # Stream-Like](rdoc-ref:Dir@Dir+As+Stream-Like):
559
885
  #
560
- # d = Dir.new("testdir")
561
- # d.tell #=> 0
562
- # d.read #=> "."
563
- # d.tell #=> 12
886
+ # dir = Dir.new('example')
887
+ # dir.tell # => 0
888
+ # dir.read # => "."
889
+ # dir.tell # => 1
564
890
  #
565
891
  def pos: () -> Integer
566
892
 
567
893
  # <!--
568
894
  # rdoc-file=dir.c
569
- # - dir.pos = integer -> integer
895
+ # - pos = position -> integer
570
896
  # -->
571
- # Synonym for Dir#seek, but returns the position parameter.
897
+ # Sets the position in `self` and returns `position`. The value of `position`
898
+ # should have been returned from an earlier call to #tell; if not, the return
899
+ # values from subsequent calls to #read are unspecified.
572
900
  #
573
- # d = Dir.new("testdir") #=> #<Dir:0x401b3c40>
574
- # d.read #=> "."
575
- # i = d.pos #=> 12
576
- # d.read #=> ".."
577
- # d.pos = i #=> 12
578
- # d.read #=> ".."
901
+ # See [Dir As Stream-Like](rdoc-ref:Dir@Dir+As+Stream-Like).
902
+ #
903
+ # Examples:
904
+ #
905
+ # dir = Dir.new('example')
906
+ # dir.pos # => 0
907
+ # dir.pos = 3 # => 3
908
+ # dir.pos # => 3
909
+ # dir.pos = 30 # => 30
910
+ # dir.pos # => 5
579
911
  #
580
912
  def pos=: [U < _ToInt] (U pos) -> U
581
913
 
582
914
  # <!--
583
915
  # rdoc-file=dir.c
584
- # - dir.read -> string or nil
916
+ # - read -> string or nil
585
917
  # -->
586
- # Reads the next entry from *dir* and returns it as a string. Returns `nil` at
587
- # the end of the stream.
918
+ # Reads and returns the next entry name from `self`; returns `nil` if at
919
+ # end-of-stream; see [Dir As Stream-Like](rdoc-ref:Dir@Dir+As+Stream-Like):
588
920
  #
589
- # d = Dir.new("testdir")
590
- # d.read #=> "."
591
- # d.read #=> ".."
592
- # d.read #=> "config.h"
921
+ # dir = Dir.new('example')
922
+ # dir.read # => "."
923
+ # dir.read # => ".."
924
+ # dir.read # => "config.h"
593
925
  #
594
926
  def read: () -> String?
595
927
 
596
928
  # <!--
597
929
  # rdoc-file=dir.c
598
- # - dir.rewind -> dir
930
+ # - rewind -> self
599
931
  # -->
600
- # Repositions *dir* to the first entry.
932
+ # Sets the position in `self` to zero; see [Dir As
933
+ # Stream-Like](rdoc-ref:Dir@Dir+As+Stream-Like):
601
934
  #
602
- # d = Dir.new("testdir")
603
- # d.read #=> "."
604
- # d.rewind #=> #<Dir:0x401b3fb0>
605
- # d.read #=> "."
935
+ # dir = Dir.new('example')
936
+ # dir.read # => "."
937
+ # dir.read # => ".."
938
+ # dir.pos # => 2
939
+ # dir.rewind # => #<Dir:example>
940
+ # dir.pos # => 0
606
941
  #
607
942
  def rewind: () -> self
608
943
 
609
944
  # <!--
610
945
  # rdoc-file=dir.c
611
- # - dir.seek( integer ) -> dir
946
+ # - seek(position) -> self
612
947
  # -->
613
- # Seeks to a particular location in *dir*. *integer* must be a value returned by
614
- # Dir#tell.
948
+ # Sets the position in `self` and returns `self`. The value of `position` should
949
+ # have been returned from an earlier call to #tell; if not, the return values
950
+ # from subsequent calls to #read are unspecified.
951
+ #
952
+ # See [Dir As Stream-Like](rdoc-ref:Dir@Dir+As+Stream-Like).
953
+ #
954
+ # Examples:
615
955
  #
616
- # d = Dir.new("testdir") #=> #<Dir:0x401b3c40>
617
- # d.read #=> "."
618
- # i = d.tell #=> 12
619
- # d.read #=> ".."
620
- # d.seek(i) #=> #<Dir:0x401b3c40>
621
- # d.read #=> ".."
956
+ # dir = Dir.new('example')
957
+ # dir.pos # => 0
958
+ # dir.seek(3) # => #<Dir:example>
959
+ # dir.pos # => 3
960
+ # dir.seek(30) # => #<Dir:example>
961
+ # dir.pos # => 5
622
962
  #
623
963
  def seek: (int pos) -> self
624
964
 
625
965
  # <!--
626
966
  # rdoc-file=dir.c
627
- # - dir.pos -> integer
628
- # - dir.tell -> integer
967
+ # - tell -> integer
629
968
  # -->
630
- # Returns the current position in *dir*. See also Dir#seek.
969
+ # Returns the current position of `self`; see [Dir As
970
+ # Stream-Like](rdoc-ref:Dir@Dir+As+Stream-Like):
631
971
  #
632
- # d = Dir.new("testdir")
633
- # d.tell #=> 0
634
- # d.read #=> "."
635
- # d.tell #=> 12
972
+ # dir = Dir.new('example')
973
+ # dir.tell # => 0
974
+ # dir.read # => "."
975
+ # dir.tell # => 1
636
976
  #
637
977
  alias tell pos
638
978
 
639
979
  # <!-- rdoc-file=dir.c -->
640
- # Returns the path parameter passed to *dir*'s constructor.
980
+ # Returns the `dirpath` string that was used to create `self` (or `nil` if
981
+ # created by method Dir.for_fd):
641
982
  #
642
- # d = Dir.new("..")
643
- # d.path #=> ".."
983
+ # Dir.new('example').path # => "example"
644
984
  #
645
985
  alias to_path path
646
986
  end