ahoward wrote:
rubyists-
does anyone know of a way to do
if filename.glob_match ‘*.txt’
…stuff
end
in ruby?
Dir#/#glob are the closest thing to this functionality - but work only if
the file were to actually exist.
Try fnmatch (in 1.8)
dave[StarterKit/SCC 14:49:48] ri fnmatch
This is a test ‘ri’. Please report errors and omissions
on http://www.rubygarden.org/ruby?RIOnePointEight
---------------------------------------------------------- File::fnmatch
File.fnmatch( aPattern, aPath, [flags] ) → (true or false)
···
Returns true if aPath matches against aPattern The pattern is not a
regular expression; instead it follows rules similar to shell
filename globbing. It may contain the following metacharacters:
flags is a bitwise OR of the FNM_xxx parameters listed in on page
unknown. The same glob pattern and flags are used by Dir::glob.
File.fnmatch('cat', 'cat') #=> true
File.fnmatch('cat', 'category') #=> false
File.fnmatch('c{at,ub}s', 'cats') #=> false
File.fnmatch('c{at,ub}s', 'cubs') #=> false
File.fnmatch('c{at,ub}s', 'cat') #=> false
File.fnmatch('c?t', 'cat') #=> true
File.fnmatch('c\?t', 'cat') #=> false
File.fnmatch('c??t', 'cat') #=> false
File.fnmatch('c*', 'cats') #=> true
File.fnmatch('c/**/t', 'c/a/b/c/t') #=> true
File.fnmatch('c*t', 'cat') #=> true
File.fnmatch('c\at', 'cat') #=> true
File.fnmatch('c\at', 'cat', File::FNM_NOESCAPE) #=> false
File.fnmatch('a?b', 'a/b') #=> true
File.fnmatch('a?b', 'a/b', File::FNM_PATHNAME) #=> false
File.fnmatch('*', '.profile') #=> false
File.fnmatch('*', '.profile', File::FNM_DOTMATCH) #=> true
File.fnmatch('*', 'dave/.profile') #=> true
File.fnmatch('*', 'dave/.profile', File::FNM_DOTMATCH) #=> true
File.fnmatch('*', 'dave/.profile', File::FNM_PATHNAME) #=>
false
File.fnmatch(‘/’, ‘dave/.profile’, File::FNM_PATHNAME) #=>
false
STRICT = File::FNM_PATHNAME | File::FNM_DOTMATCH
File.fnmatch(‘/’, ‘dave/.profile’, STRICT) #=> true