Method: Dir.glob
- Defined in:
- dir.c
.glob(pattern, [flags]) ⇒ Array .glob(pattern, [flags]) {|filename| ... } ⇒ nil
Returns the filenames found by expanding pattern which is an Array
of the patterns or the pattern String
, either as an array or as parameters to the block. Note that this pattern is not a regexp (it's closer to a shell glob). See File::fnmatch
for the meaning of the flags parameter. Note that case sensitivity depends on your system (so File::FNM_CASEFOLD
is ignored)
*
-
Matches any file. Can be restricted by other values in the glob.
*
will match all files;c*
will match all files beginning withc
;*c
will match all files ending withc
; and*c*
will match all files that havec
in them (including at the beginning or end). Equivalent to/ .* /x
in regexp. Note, this will not match Unix-like hidden files (dotfiles). In order to include those in the match results, you must use something like "*,*,.*". **
-
Matches directories recursively.
?
-
Matches any one character. Equivalent to
/.{1}/
in regexp. [set]
-
Matches any one character in
set
. Behaves exactly like character sets in Regexp, including set negation ([^a-z]
). {p,q}
-
Matches either literal
p
or literalq
. Matching literals may be more than one character in length. More than two literals may be specified. Equivalent to pattern alternation in regexp. - <code></code>
-
Escapes the next metacharacter. Note that this means you cannot use backslash in windows as part of a glob, i.e. Dir will not work use Dir instead
Dir["config.?"] #=> ["config.h"]
Dir.glob("config.?") #=> ["config.h"]
Dir.glob("*.[a-z][a-z]") #=> ["main.rb"]
Dir.glob("*.[^r]*") #=> ["config.h"]
Dir.glob("*.{rb,h}") #=> ["main.rb", "config.h"]
Dir.glob("*") #=> ["config.h", "main.rb"]
Dir.glob("*", File::FNM_DOTMATCH) #=> [".", "..", "config.h", "main.rb"]
rbfiles = File.join("**", "*.rb")
Dir.glob(rbfiles) #=> ["main.rb",
# "lib/song.rb",
# "lib/song/karaoke.rb"]
libdirs = File.join("**", "lib")
Dir.glob(libdirs) #=> ["lib"]
librbfiles = File.join("**", "lib", "**", "*.rb")
Dir.glob(librbfiles) #=> ["lib/song.rb",
# "lib/song/karaoke.rb"]
librbfiles = File.join("**", "lib", "*.rb")
Dir.glob(librbfiles) #=> ["lib/song.rb"]
|
# File 'dir.c'
/*
* call-seq:
* Dir.glob( pattern, [flags] ) -> array
* Dir.glob( pattern, [flags] ) {| filename | block } -> nil
*
* Returns the filenames found by expanding <i>pattern</i> which is
* an +Array+ of the patterns or the pattern +String+, either as an
* <i>array</i> or as parameters to the block. Note that this pattern
* is not a regexp (it's closer to a shell glob). See
* <code>File::fnmatch</code> for the meaning of the <i>flags</i>
* parameter. Note that case sensitivity depends on your system (so
* <code>File::FNM_CASEFOLD</code> is ignored)
*
* <code>*</code>:: Matches any file. Can be restricted by
* other values in the glob. <code>*</code>
* will match all files; <code>c*</code> will
* match all files beginning with
* <code>c</code>; <code>*c</code> will match
* all files ending with <code>c</code>; and
* <code>\*c\*</code> will match all files that
* have <code>c</code> in them (including at
* the beginning or end). Equivalent to
* <code>/ .* /x</code> in regexp. Note, this
* will not match Unix-like hidden files (dotfiles).
* In order to include those in the match results,
* you must use something like "{*,.*}".
* <code>**</code>:: Matches directories recursively.
* <code>?</code>:: Matches any one character. Equivalent to
* <code>/.{1}/</code> in regexp.
* <code>[set]</code>:: Matches any one character in +set+.
* Behaves exactly like character sets in
* Regexp, including set negation
* (<code>[^a-z]</code>).
* <code>{p,q}</code>:: Matches either literal <code>p</code> or
* literal <code>q</code>. Matching literals
* may be more than one character in length.
* More than two literals may be specified.
* Equivalent to pattern alternation in
* regexp.
* <code>\</code>:: Escapes the next metacharacter.
* Note that this means you cannot use backslash in windows
* as part of a glob, i.e. Dir["c:\\foo*"] will not work
* use Dir["c:/foo*"] instead
*
* Dir["config.?"] #=> ["config.h"]
* Dir.glob("config.?") #=> ["config.h"]
* Dir.glob("*.[a-z][a-z]") #=> ["main.rb"]
* Dir.glob("*.[^r]*") #=> ["config.h"]
* Dir.glob("*.{rb,h}") #=> ["main.rb", "config.h"]
* Dir.glob("*") #=> ["config.h", "main.rb"]
* Dir.glob("*", File::FNM_DOTMATCH) #=> [".", "..", "config.h", "main.rb"]
*
* rbfiles = File.join("**", "*.rb")
* Dir.glob(rbfiles) #=> ["main.rb",
* # "lib/song.rb",
* # "lib/song/karaoke.rb"]
* libdirs = File.join("**", "lib")
* Dir.glob(libdirs) #=> ["lib"]
*
* librbfiles = File.join("**", "lib", "**", "*.rb")
* Dir.glob(librbfiles) #=> ["lib/song.rb",
* # "lib/song/karaoke.rb"]
*
* librbfiles = File.join("**", "lib", "*.rb")
* Dir.glob(librbfiles) #=> ["lib/song.rb"]
*/
static VALUE
dir_s_glob(int argc, VALUE *argv, VALUE obj)
{
VALUE str, rflags, ary;
int flags;
if (rb_scan_args(argc, argv, "11", &str, &rflags) == 2)
flags = NUM2INT(rflags);
else
flags = 0;
ary = rb_check_array_type(str);
if (NIL_P(ary)) {
ary = rb_push_glob(str, flags);
}
else {
volatile VALUE v = ary;
ary = dir_globs(RARRAY_LEN(v), RARRAY_PTR(v), flags);
}
if (rb_block_given_p()) {
rb_ary_each(ary);
return Qnil;
}
return ary;
}
|