Glob but not fnmatch?


#1

Can’t imagine this is right (Linux, Ruby 1.8.4)

Dir.glob(‘lib/**/*’) #=> [ ‘lib/facets.rb’ ]

File.fnmatch?(‘lib/**/*’, ‘lib/facets.rb’) #=> false

In the mean time, anyone got a working Ruby-coded #fnmatch? I can use?

T.


#2

Hi!

It does seem strange…

JRuby does it like this in Java, it should be exceedingly trivial to
convert to Ruby:

    checkArgumentCount(args, 2, -1);
    String pattern = args[0].convertToString().toString();
    RubyString path = args[1].convertToString();
    int opts = (int) (args.length > 2 ?

args[2].convertToInteger().getLongValue() : 0);

    boolean dot = pattern.startsWith(".");

    pattern = pattern.replaceAll("(\\.)", "\\\\$1");
    pattern = pattern.replaceAll("(?<=[^\\\\])\\*", ".*");
    pattern = pattern.replaceAll("^\\*", ".*");
    pattern = pattern.replaceAll("(?<=[^\\\\])\\?", ".");
    pattern = pattern.replaceAll("^\\?", ".");
    if ((opts & FNM_NOESCAPE) != FNM_NOESCAPE) {
        pattern = pattern.replaceAll("\\\\([^\\\\*\\\\?])", "$1");
    }
    pattern = pattern.replaceAll("\\{", "\\\\{");
    pattern = pattern.replaceAll("\\}", "\\\\}");
    pattern = "^" + pattern + "$";

    if (path.toString().startsWith(".") && !dot) {
        return getRuntime().newBoolean(false);
    }

    return getRuntime().newBoolean(Pattern.matches(pattern,

path.toString()));

Regards
Ola B.

----- Original Message -----
From: removed_email_address@domain.invalid
Date: Thursday, June 8, 2006 2:02 pm
Subject: glob but not fnmatch?
To: removed_email_address@domain.invalid (ruby-talk ML)


#3

2006/6/8, removed_email_address@domain.invalid removed_email_address@domain.invalid:

Can’t imagine this is right (Linux, Ruby 1.8.4)

Dir.glob(‘lib/**/*’) #=> [ ‘lib/facets.rb’ ]

File.fnmatch?(‘lib/**/*’, ‘lib/facets.rb’) #=> false

In the mean time, anyone got a working Ruby-coded #fnmatch? I can use?

As far as I can see File.fnmatch does not support directory path
matching with “**”:
http://www.ruby-doc.org/core/classes/File.html#M000012

So although this may seem inconvenient / asymmetric behavior conforms
at least to the docs.

robert