File.fnmatch and **

  • Thread starter Thomas Sondergaard
  • Start date
T

Thomas Sondergaard

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
 
N

nobu.nokada

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
 
T

Thomas Sondergaard

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

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
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: File.fnmatch and **"

|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.
 
H

H.Yamamoto

Hi.

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?
 
T

Thomas Sondergaard

H.Yamamoto said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top