rbs 3.3.2 → 3.4.0.pre.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/comments.yml +2 -5
- data/.github/workflows/ruby.yml +7 -8
- data/.github/workflows/typecheck.yml +37 -0
- data/CHANGELOG.md +50 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +11 -11
- data/Steepfile +2 -2
- data/core/array.rbs +19 -49
- data/core/basic_object.rbs +2 -2
- data/core/comparable.rbs +17 -8
- data/core/complex.rbs +82 -43
- data/core/data.rbs +2 -4
- data/core/dir.rbs +635 -295
- data/core/enumerable.rbs +11 -18
- data/core/enumerator.rbs +37 -31
- data/core/errors.rbs +4 -0
- data/core/false_class.rbs +34 -15
- data/core/fiber.rbs +23 -0
- data/core/file.rbs +329 -120
- data/core/float.rbs +17 -32
- data/core/gc.rbs +17 -11
- data/core/hash.rbs +22 -44
- data/core/integer.rbs +82 -113
- data/core/io/buffer.rbs +90 -47
- data/core/io.rbs +39 -116
- data/core/kernel.rbs +442 -489
- data/core/match_data.rbs +55 -56
- data/core/module.rbs +45 -1
- data/core/nil_class.rbs +98 -35
- data/core/numeric.rbs +22 -32
- data/core/object_space/weak_key_map.rbs +102 -0
- data/core/process.rbs +1242 -655
- data/core/ractor.rbs +139 -120
- data/core/range.rbs +100 -4
- data/core/rational.rbs +0 -4
- data/core/rbs/unnamed/argf.rbs +16 -8
- data/core/rbs/unnamed/env_class.rbs +0 -24
- data/core/refinement.rbs +8 -0
- data/core/regexp.rbs +1149 -598
- data/core/ruby_vm.rbs +126 -12
- data/core/rubygems/platform.rbs +9 -0
- data/core/rubygems/rubygems.rbs +1 -1
- data/core/rubygems/version.rbs +5 -1
- data/core/set.rbs +20 -22
- data/core/signal.rbs +4 -4
- data/core/string.rbs +283 -230
- data/core/string_io.rbs +2 -14
- data/core/struct.rbs +404 -24
- data/core/symbol.rbs +1 -19
- data/core/thread.rbs +29 -12
- data/core/time.rbs +227 -104
- data/core/trace_point.rbs +2 -5
- data/core/true_class.rbs +54 -21
- data/core/warning.rbs +14 -11
- data/docs/data_and_struct.md +29 -0
- data/docs/syntax.md +3 -5
- data/docs/tools.md +1 -0
- data/ext/rbs_extension/lexer.c +643 -559
- data/ext/rbs_extension/lexer.re +5 -1
- data/ext/rbs_extension/parser.c +12 -3
- data/ext/rbs_extension/unescape.c +7 -47
- data/lib/rbs/cli/diff.rb +4 -1
- data/lib/rbs/cli/validate.rb +280 -0
- data/lib/rbs/cli.rb +2 -194
- data/lib/rbs/collection/config.rb +5 -6
- data/lib/rbs/collection/sources/git.rb +1 -1
- data/lib/rbs/collection.rb +1 -0
- data/lib/rbs/diff.rb +7 -4
- data/lib/rbs/errors.rb +11 -0
- data/lib/rbs/test/errors.rb +4 -1
- data/lib/rbs/test/guaranteed.rb +2 -3
- data/lib/rbs/test/type_check.rb +15 -10
- data/lib/rbs/test.rb +3 -3
- data/lib/rbs/types.rb +29 -0
- data/lib/rbs/unit_test/convertibles.rb +176 -0
- data/lib/rbs/unit_test/spy.rb +136 -0
- data/lib/rbs/unit_test/type_assertions.rb +341 -0
- data/lib/rbs/unit_test/with_aliases.rb +143 -0
- data/lib/rbs/unit_test.rb +6 -0
- data/lib/rbs/version.rb +1 -1
- data/sig/cli/validate.rbs +43 -0
- data/sig/diff.rbs +3 -1
- data/sig/errors.rbs +8 -0
- data/sig/rbs.rbs +1 -1
- data/sig/test/errors.rbs +52 -0
- data/sig/test/guranteed.rbs +9 -0
- data/sig/test/type_check.rbs +19 -0
- data/sig/test.rbs +82 -0
- data/sig/types.rbs +6 -1
- data/sig/unit_test/convertibles.rbs +154 -0
- data/sig/unit_test/spy.rbs +28 -0
- data/sig/unit_test/type_assertions.rbs +194 -0
- data/sig/unit_test/with_aliases.rbs +136 -0
- data/stdlib/base64/0/base64.rbs +307 -45
- data/stdlib/bigdecimal/0/big_decimal.rbs +35 -15
- data/stdlib/coverage/0/coverage.rbs +2 -2
- data/stdlib/csv/0/csv.rbs +25 -55
- data/stdlib/date/0/date.rbs +1 -43
- data/stdlib/date/0/date_time.rbs +1 -13
- data/stdlib/delegate/0/delegator.rbs +186 -0
- data/stdlib/delegate/0/kernel.rbs +47 -0
- data/stdlib/delegate/0/simple_delegator.rbs +98 -0
- data/stdlib/did_you_mean/0/did_you_mean.rbs +1 -1
- data/stdlib/erb/0/erb.rbs +2 -2
- data/stdlib/fileutils/0/fileutils.rbs +0 -19
- data/stdlib/io-console/0/io-console.rbs +12 -1
- data/stdlib/ipaddr/0/ipaddr.rbs +2 -1
- data/stdlib/json/0/json.rbs +320 -81
- data/stdlib/logger/0/logger.rbs +9 -5
- data/stdlib/monitor/0/monitor.rbs +78 -0
- data/stdlib/net-http/0/net-http.rbs +1880 -543
- data/stdlib/objspace/0/objspace.rbs +19 -13
- data/stdlib/openssl/0/openssl.rbs +508 -127
- data/stdlib/optparse/0/optparse.rbs +25 -11
- data/stdlib/pathname/0/pathname.rbs +1 -1
- data/stdlib/pp/0/pp.rbs +2 -5
- data/stdlib/prettyprint/0/prettyprint.rbs +2 -2
- data/stdlib/pstore/0/pstore.rbs +2 -4
- data/stdlib/rdoc/0/comment.rbs +1 -2
- data/stdlib/resolv/0/resolv.rbs +4 -2
- data/stdlib/socket/0/socket.rbs +2 -2
- data/stdlib/socket/0/unix_socket.rbs +2 -2
- data/stdlib/strscan/0/string_scanner.rbs +3 -2
- data/stdlib/tempfile/0/tempfile.rbs +1 -1
- data/stdlib/uri/0/common.rbs +245 -123
- metadata +23 -4
- data/lib/rbs/test/spy.rb +0 -6
data/core/dir.rbs
CHANGED
@@ -1,11 +1,85 @@
|
|
1
1
|
# <!-- rdoc-file=dir.rb -->
|
2
|
-
#
|
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
|
-
#
|
7
|
-
#
|
8
|
-
#
|
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(
|
98
|
-
# - Dir.new(
|
171
|
+
# - Dir.new(dirpath) -> dir
|
172
|
+
# - Dir.new(dirpath, encoding: nil) -> dir
|
99
173
|
# -->
|
100
|
-
# Returns a new
|
174
|
+
# Returns a new Dir object for the directory at `dirpath`:
|
175
|
+
#
|
176
|
+
# Dir.new('.') # => #<Dir:.>
|
101
177
|
#
|
102
|
-
# The optional
|
103
|
-
# directory
|
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[
|
189
|
+
# - Dir[*patterns, base: nil, sort: true] -> array
|
110
190
|
# -->
|
111
|
-
#
|
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(
|
118
|
-
# - Dir.chdir
|
119
|
-
#
|
120
|
-
#
|
121
|
-
#
|
122
|
-
#
|
123
|
-
#
|
124
|
-
#
|
125
|
-
#
|
126
|
-
#
|
127
|
-
#
|
128
|
-
#
|
129
|
-
#
|
130
|
-
#
|
131
|
-
#
|
132
|
-
#
|
133
|
-
#
|
134
|
-
#
|
135
|
-
#
|
136
|
-
#
|
137
|
-
#
|
138
|
-
#
|
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
|
-
#
|
250
|
+
# Dir.pwd # => "/tmp"
|
141
251
|
# end
|
142
|
-
#
|
252
|
+
# Dir.pwd # => "/var/spool/mail"
|
143
253
|
#
|
144
|
-
#
|
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
|
-
#
|
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(
|
158
|
-
# - Dir.children(
|
265
|
+
# - Dir.children(dirpath) -> array
|
266
|
+
# - Dir.children(dirpath, encoding: 'UTF-8') -> array
|
159
267
|
# -->
|
160
|
-
# Returns an array
|
161
|
-
#
|
162
|
-
#
|
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
|
-
#
|
165
|
-
# directory. If not specified, the filesystem encoding is used.
|
277
|
+
# See [String Encoding](rdoc-ref:encodings.rdoc@String+Encoding).
|
166
278
|
#
|
167
|
-
#
|
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(
|
285
|
+
# - Dir.chroot(dirpath) -> 0
|
174
286
|
# -->
|
175
|
-
# Changes
|
176
|
-
#
|
177
|
-
#
|
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.
|
184
|
-
# - Dir.rmdir( string ) -> 0
|
185
|
-
# - Dir.unlink( string ) -> 0
|
299
|
+
# - Dir.rmdir(dirpath) -> 0
|
186
300
|
# -->
|
187
|
-
#
|
188
|
-
#
|
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(
|
195
|
-
# - Dir.each_child(
|
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
|
-
#
|
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?(
|
321
|
+
# - Dir.empty?(dirpath) -> true or false
|
217
322
|
# -->
|
218
|
-
# Returns `
|
219
|
-
#
|
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(
|
226
|
-
# - Dir.entries( dirname, encoding: enc ) -> array
|
338
|
+
# - Dir.entries(dirname, encoding: 'UTF-8') -> array
|
227
339
|
# -->
|
228
|
-
# Returns an array
|
229
|
-
#
|
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
|
-
#
|
232
|
-
#
|
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
|
-
#
|
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?(
|
357
|
+
# - Dir.exist?(dirpath) -> true or false
|
241
358
|
# -->
|
242
|
-
# Returns `
|
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.
|
249
|
-
# - Dir.
|
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
|
-
#
|
254
|
-
#
|
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
|
-
#
|
441
|
+
# Encoding:
|
257
442
|
#
|
258
|
-
# Dir.foreach(
|
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
|
-
#
|
446
|
+
# Output:
|
261
447
|
#
|
262
|
-
#
|
263
|
-
#
|
264
|
-
#
|
265
|
-
#
|
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.
|
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
|
483
|
+
# Returns the path to the current working directory:
|
275
484
|
#
|
276
|
-
# Dir.chdir("/tmp")
|
277
|
-
# Dir.
|
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(
|
285
|
-
# - Dir.glob(
|
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
|
-
#
|
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
|
-
#
|
293
|
-
#
|
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
|
-
#
|
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
|
-
#
|
302
|
-
#
|
303
|
-
#
|
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
|
-
# :
|
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
|
-
#
|
321
|
-
#
|
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
|
-
#
|
332
|
-
#
|
333
|
-
#
|
520
|
+
# config.h
|
521
|
+
# lib
|
522
|
+
# main.rb
|
334
523
|
#
|
335
|
-
# `
|
336
|
-
#
|
337
|
-
# alternation in regexp.
|
524
|
+
# If optional keyword argument `flags` is given, the value modifies the
|
525
|
+
# matching; see below.
|
338
526
|
#
|
339
|
-
#
|
340
|
-
#
|
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
|
-
#
|
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
|
-
#
|
346
|
-
#
|
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
|
-
#
|
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
|
352
|
-
#
|
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(
|
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(
|
365
|
-
# # "song/karaoke.rb"]
|
621
|
+
# Dir.glob('**/lib') # => ["lib"]
|
366
622
|
#
|
367
|
-
# Dir.glob(
|
623
|
+
# Dir.glob('**/lib/**/*.rb') # => ["lib/song/karaoke.rb", "lib/song.rb"]
|
368
624
|
#
|
369
|
-
# Dir.glob(
|
370
|
-
# # "lib/song/karaoke.rb"]
|
625
|
+
# Dir.glob('**/lib/*.rb') # => ["lib/song.rb"]
|
371
626
|
#
|
372
|
-
#
|
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()
|
380
|
-
# - Dir.home("root") -> "/root"
|
670
|
+
# - Dir.home(user_name = nil) -> dirpath
|
381
671
|
# -->
|
382
|
-
#
|
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(
|
684
|
+
# - Dir.mkdir(dirpath, permissions = 0775) -> 0
|
389
685
|
# -->
|
390
|
-
#
|
391
|
-
#
|
392
|
-
#
|
393
|
-
#
|
394
|
-
#
|
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
|
-
#
|
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(
|
403
|
-
# - Dir.open(
|
404
|
-
# - Dir.open(
|
405
|
-
# - Dir.open(
|
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
|
-
#
|
408
|
-
#
|
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
|
-
#
|
411
|
-
#
|
412
|
-
#
|
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
|
731
|
+
# Returns the path to the current working directory:
|
423
732
|
#
|
424
|
-
# Dir.chdir("/tmp")
|
425
|
-
# Dir.
|
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.
|
433
|
-
# - Dir.rmdir( string ) -> 0
|
434
|
-
# - Dir.unlink( string ) -> 0
|
740
|
+
# - Dir.rmdir(dirpath) -> 0
|
435
741
|
# -->
|
436
|
-
#
|
437
|
-
#
|
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.
|
444
|
-
# - Dir.rmdir( string ) -> 0
|
445
|
-
# - Dir.unlink( string ) -> 0
|
752
|
+
# - Dir.rmdir(dirpath) -> 0
|
446
753
|
# -->
|
447
|
-
#
|
448
|
-
#
|
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
|
-
# -
|
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
|
459
|
-
# this directory.
|
782
|
+
# Returns an array of the entry names in `self` except for `'.'` and `'..'`:
|
460
783
|
#
|
461
|
-
#
|
462
|
-
#
|
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
|
-
# -
|
791
|
+
# - close -> nil
|
469
792
|
# -->
|
470
|
-
# Closes the
|
471
|
-
#
|
793
|
+
# Closes the stream in `self`, if it is open, and returns `nil`; ignored if
|
794
|
+
# `self` is already closed:
|
472
795
|
#
|
473
|
-
#
|
474
|
-
#
|
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
|
-
# -
|
481
|
-
# - dir.each -> an_enumerator
|
806
|
+
# - each {|entry_name| ... } -> self
|
482
807
|
# -->
|
483
|
-
# Calls the block
|
484
|
-
# each entry as a parameter to the block.
|
808
|
+
# Calls the block with each entry name in `self`:
|
485
809
|
#
|
486
|
-
#
|
810
|
+
# Dir.new('example').each {|entry_name| p entry_name }
|
487
811
|
#
|
488
|
-
#
|
489
|
-
# d.each {|x| puts "Got #{x}" }
|
812
|
+
# Output:
|
490
813
|
#
|
491
|
-
#
|
814
|
+
# "."
|
815
|
+
# ".."
|
816
|
+
# "config.h"
|
817
|
+
# "lib"
|
818
|
+
# "main.rb"
|
492
819
|
#
|
493
|
-
#
|
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
|
-
# -
|
504
|
-
# - dir.each_child -> an_enumerator
|
827
|
+
# - each_child {|entry_name| ... } -> self
|
505
828
|
# -->
|
506
|
-
# Calls the block
|
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
|
-
#
|
831
|
+
# dir = Dir.new('/example')
|
832
|
+
# dir.each_child {|entry_name| p entry_name }
|
510
833
|
#
|
511
|
-
#
|
512
|
-
# d.each_child {|x| puts "Got #{x}" }
|
834
|
+
# Output:
|
513
835
|
#
|
514
|
-
#
|
836
|
+
# "config.h"
|
837
|
+
# "lib"
|
838
|
+
# "main.rb"
|
515
839
|
#
|
516
|
-
#
|
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
|
-
# -
|
847
|
+
# - fileno -> integer
|
525
848
|
# -->
|
526
849
|
# Returns the file descriptor used in *dir*.
|
527
850
|
#
|
528
|
-
# d = Dir.new(
|
529
|
-
# d.fileno
|
851
|
+
# d = Dir.new('..')
|
852
|
+
# d.fileno # => 8
|
530
853
|
#
|
531
|
-
# This method uses
|
532
|
-
#
|
533
|
-
#
|
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
|
-
# -
|
863
|
+
# - inspect -> string
|
540
864
|
# -->
|
541
|
-
#
|
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
|
-
# -
|
548
|
-
# - dir.to_path -> string or nil
|
873
|
+
# - path -> string or nil
|
549
874
|
# -->
|
550
|
-
# Returns the
|
875
|
+
# Returns the `dirpath` string that was used to create `self` (or `nil` if
|
876
|
+
# created by method Dir.for_fd):
|
551
877
|
#
|
552
|
-
#
|
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
|
883
|
+
# Returns the current position of `self`; see [Dir As
|
884
|
+
# Stream-Like](rdoc-ref:Dir@Dir+As+Stream-Like):
|
559
885
|
#
|
560
|
-
#
|
561
|
-
#
|
562
|
-
#
|
563
|
-
#
|
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
|
-
# -
|
895
|
+
# - pos = position -> integer
|
570
896
|
# -->
|
571
|
-
#
|
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
|
-
#
|
574
|
-
#
|
575
|
-
#
|
576
|
-
#
|
577
|
-
#
|
578
|
-
#
|
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
|
-
# -
|
916
|
+
# - read -> string or nil
|
585
917
|
# -->
|
586
|
-
# Reads the next entry from
|
587
|
-
#
|
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
|
-
#
|
590
|
-
#
|
591
|
-
#
|
592
|
-
#
|
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
|
-
# -
|
930
|
+
# - rewind -> self
|
599
931
|
# -->
|
600
|
-
#
|
932
|
+
# Sets the position in `self` to zero; see [Dir As
|
933
|
+
# Stream-Like](rdoc-ref:Dir@Dir+As+Stream-Like):
|
601
934
|
#
|
602
|
-
#
|
603
|
-
#
|
604
|
-
#
|
605
|
-
#
|
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
|
-
# -
|
946
|
+
# - seek(position) -> self
|
612
947
|
# -->
|
613
|
-
#
|
614
|
-
#
|
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
|
-
#
|
617
|
-
#
|
618
|
-
#
|
619
|
-
#
|
620
|
-
#
|
621
|
-
#
|
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
|
-
# -
|
628
|
-
# - dir.tell -> integer
|
967
|
+
# - tell -> integer
|
629
968
|
# -->
|
630
|
-
# Returns the current position
|
969
|
+
# Returns the current position of `self`; see [Dir As
|
970
|
+
# Stream-Like](rdoc-ref:Dir@Dir+As+Stream-Like):
|
631
971
|
#
|
632
|
-
#
|
633
|
-
#
|
634
|
-
#
|
635
|
-
#
|
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
|
980
|
+
# Returns the `dirpath` string that was used to create `self` (or `nil` if
|
981
|
+
# created by method Dir.for_fd):
|
641
982
|
#
|
642
|
-
#
|
643
|
-
# d.path #=> ".."
|
983
|
+
# Dir.new('example').path # => "example"
|
644
984
|
#
|
645
985
|
alias to_path path
|
646
986
|
end
|