File.fnmatch and **

Dir.glob('**/*') matches directories recursively, but File.fnmatch('**/*', "a/b/c", File::FNM_PATHNAME) returns false. I thought (from reading the docs at ruby-doc.org) that the patterns are supposed to be the same.

Can I match directories recursively with File.fnmatch?

Thomas

Hi,

At Fri, 1 Apr 2005 18:54:44 +0900,
Thomas Sondergaard wrote in [ruby-talk:136264]:

Dir.glob('**/*') matches directories recursively, but
File.fnmatch('**/*', "a/b/c", File::FNM_PATHNAME) returns false. I
thought (from reading the docs at ruby-doc.org) that the patterns are
supposed to be the same.

  $ ruby18 -v -e 'p File.fnmatch("**/*", "a/b/c")'
  ruby 1.8.2 (2005-03-31) [i686-linux]
  true

···

--
Nobu Nakada

Hi.

(2005/04/01 18:54)

Dir.glob('**/*') matches directories recursively, but
File.fnmatch('**/*', "a/b/c", File::FNM_PATHNAME) returns false. I
thought (from reading the docs at ruby-doc.org) that the patterns are
supposed to be the same.

Can I match directories recursively with File.fnmatch?

Well, File.fnmatch on ruby1.9 works like that.

irb(main):002:0> File.fnmatch('**/*', 'a/b/c', File::FNM_PATHNAME)
=> true
irb(main):003:0> File.fnmatch('a**/*', 'a/b/c', File::FNM_PATHNAME)
=> false
irb(main):004:0> File.fnmatch('**/b*', 'a/b/c', File::FNM_PATHNAME)
=> false
irb(main):005:0> File.fnmatch('**/*', '/a/b/c', File::FNM_PATHNAME)
=> true
irb(main):006:0> File.fnmatch('**/*', 'c:/a/b/c', File::FNM_PATHNAME)
=> true

Does this fit your need?

···

Thomas Sondergaard <ts_news1@sondergaard.cc> wrote:

Yes, well the following returns true as well, which is not desirable. I need to pass the File::FNM_PATHNAME for fnmatch to treat the string as a path and not a file name.

[ts@argon qtruby-1.0.8]$ ruby -v -e 'p File.fnmatch("*", "a/b/c")'
ruby 1.8.2 (2004-12-25) [i686-linux]
true

I just wonder why only Dir.glob understands the ** pattern.

Thomas

···

nobu.nokada@softhome.net wrote:

Hi,

At Fri, 1 Apr 2005 18:54:44 +0900,
Thomas Sondergaard wrote in [ruby-talk:136264]:

Dir.glob('**/*') matches directories recursively, but File.fnmatch('**/*', "a/b/c", File::FNM_PATHNAME) returns false. I thought (from reading the docs at ruby-doc.org) that the patterns are supposed to be the same.

  $ ruby18 -v -e 'p File.fnmatch("**/*", "a/b/c")'
  ruby 1.8.2 (2005-03-31) [i686-linux]
  true

H.Yamamoto wrote:

Well, File.fnmatch on ruby1.9 works like that.

irb(main):002:0> File.fnmatch('**/*', 'a/b/c', File::FNM_PATHNAME)
=> true
irb(main):003:0> File.fnmatch('a**/*', 'a/b/c', File::FNM_PATHNAME)
=> false
irb(main):004:0> File.fnmatch('**/b*', 'a/b/c', File::FNM_PATHNAME)
=> false
irb(main):005:0> File.fnmatch('**/*', '/a/b/c', File::FNM_PATHNAME)
=> true
irb(main):006:0> File.fnmatch('**/*', 'c:/a/b/c', File::FNM_PATHNAME)
=> true

Does this fit your need?

Yes, that's perfect!

Thanks,

Thomas

Hi,

···

In message "Re: File.fnmatch and **" on Sun, 3 Apr 2005 19:14:42 +0900, Thomas Sondergaard <ts_news1@sondergaard.cc> writes:

I just wonder why only Dir.glob understands the ** pattern.

(a) fnmatch conforms POSIX standard which does not contain **
    pattern.

(b) besides that, there's no good implementation of fnmatch that
    supports ** pattern when FNM_PATHNAME is specified.

              matz.