Shell glob match

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.

-a

···

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

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

Try fnmatch (in 1.8)

so i should stop work on my fnmatch extension then? :wink:

guess this is not in the shim - bummer - i’ll have to wait.

-a

···

On Tue, 13 May 2003, Dave Thomas wrote:

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

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Hi,

···

At Tue, 13 May 2003 06:06:07 +0900, ahoward wrote:

guess this is not in the shim - bummer - i’ll have to wait.

There’s shim/ruby16/ext/features/ruby18/file.


Nobu Nakada

took me a moment to determine you had to

ruby -r features/ruby18 -e “puts File::fnmatch ‘*.txt’, ‘foo.txt’”
^^^^^^^^^^^^^^^^^^

that could be in the ‘Description:’ on the raa…

but i got it now - thanks!

-a

···

On Tue, 13 May 2003 nobu.nokada@softhome.net wrote:

Hi,

At Tue, 13 May 2003 06:06:07 +0900, > ahoward wrote:

guess this is not in the shim - bummer - i’ll have to wait.

There’s shim/ruby16/ext/features/ruby18/file.

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================