Skip to content

Commit fdad6b5

Browse files
committed
[DOC] Restore glob documentation lost in consolidation
Dir.glob's block form, Dir[]'s multiple pattern arguments, matching only directories by a trailing slash, and the note that '**' without a following slash is equivalent to '*' were dropped when the glob documentation was consolidated into doc/file/filename_globbing.md (rubyGH-17265). Restore them, with examples verified against the current source tree.
1 parent 3a372f0 commit fdad6b5

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

dir.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,29 @@ def initialize(name, encoding: nil)
213213
end
214214

215215
# call-seq:
216-
# Dir[patterns, base: nil, sort: true] -> array
216+
# Dir[*patterns, base: nil, sort: true] -> array
217+
#
218+
# Like Dir.glob, but does not accept keyword argument +flags+,
219+
# and may take multiple patterns as arguments:
220+
#
221+
# Dir['*.rb', '*.h'].take(3) # => ["KNOWNBUGS.rb", "array.rb", "ast.rb"]
217222
#
218-
# Like Dir.glob, but does not accept keyword argument +flags+.
219223
def self.[](*args, base: nil, sort: true)
220224
Primitive.dir_s_aref(args, base, sort)
221225
end
222226

223227
# call-seq:
224228
# Dir.glob(patterns, flags: 0, base: '.', sort: true) -> array_of_entries
229+
# Dir.glob(patterns, flags: 0, base: '.', sort: true) {|entry_name| ... } -> nil
225230
#
226231
# Returns an array of filesystem entries;
227232
# see {Filename Globbing}[rdoc-ref:file/filename_globbing.md].
233+
#
234+
# With a block given, calls the block with each of the selected entry names
235+
# and returns +nil+:
236+
#
237+
# Dir.glob('*.rb') {|entry_name| puts entry_name } # => nil
238+
#
228239
def self.glob(pattern, _flags = 0, flags: _flags, base: nil, sort: true)
229240
Primitive.attr! :use_block
230241
Primitive.dir_s_glob(pattern, flags, base, sort)

doc/file/filename_globbing.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ Inputs to the filename-globbing methods:
3333

3434
Their return values:
3535

36-
- Each of the methods `Dir[]` and Dir.glob returns an array of the selected string entries.
36+
- \Method `Dir[]` returns an array of the selected string entries.
37+
- \Method Dir.glob with no block returns an array of the selected string entries.
38+
- \Method Dir.glob with a block calls the block with each selected string entry
39+
and returns `nil`.
3740
- \Method Pathname#glob with no block returns an array of Pathname objects
3841
each based on a selected string entry.
3942
- \Method Pathname#glob with a block calls the block with each pathname
@@ -44,8 +47,13 @@ Examples:
4447
```ruby
4548
Dir['*'].take(3)
4649
# => ["BSDL", "CONTRIBUTING.md", "COPYING"]
50+
Dir['*.rb', '*.h'].take(3)
51+
# => ["KNOWNBUGS.rb", "array.rb", "ast.rb"]
4752
Dir.glob('*').take(3)
4853
# => ["BSDL", "CONTRIBUTING.md", "COPYING"]
54+
Dir.glob(['*.rb', '*.h']).take(3)
55+
# => ["KNOWNBUGS.rb", "array.rb", "ast.rb"]
56+
Dir.glob('*.rb') {|entry_name| puts entry_name } # => nil
4957
Pathname('.').glob('*').take(3)
5058
# => [#<Pathname:BSDL>, #<Pathname:CONTRIBUTING.md>, #<Pathname:COPYING>]
5159
a = []
@@ -299,6 +307,25 @@ Pathname('.').glob('test/ruby/**/*.rb').take(3)
299307
# #<Pathname:test/ruby/box/a.1_1_0.rb>]
300308
```
301309

310+
The double-asterisk pattern matches directories recursively
311+
only when followed by the slash character `'/'`;
312+
otherwise it is equivalent to pattern `'*'`:
313+
314+
```ruby
315+
Dir.glob('**') == Dir.glob('*') # => true
316+
```
317+
318+
A pattern ending with `'/'` matches directory names only;
319+
each matched name ends with `'/'`:
320+
321+
```ruby
322+
# Find the directories at the top level.
323+
Dir.glob('*/').take(3) # => ["basictest/", "benchmark/", "bin/"]
324+
325+
# Find all directories everywhere.
326+
Dir.glob('**/').take(3) # => ["basictest/", "benchmark/", "benchmark/gc/"]
327+
```
328+
302329
The pattern may be escaped:
303330

304331
```ruby

0 commit comments

Comments
 (0)