Forum: Ruby glob but not fnmatch?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
45196398e9685000d195ec626d477f0e?d=identicon&s=25 unknown (Guest)
on 2006-06-08 14:02
(Received via mailing list)
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.
7359ed44852399295c6247dd9719b81b?d=identicon&s=25 Ola Bini (Guest)
on 2006-06-08 14:08
(Received via mailing list)
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 Bini

----- Original Message -----
From: transfire@gmail.com
Date: Thursday, June 8, 2006 2:02 pm
Subject: glob but not fnmatch?
To: ruby-talk@ruby-lang.org (ruby-talk ML)
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-06-08 14:49
(Received via mailing list)
2006/6/8, transfire@gmail.com <transfire@gmail.com>:
> 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
This topic is locked and can not be replied to.