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.
unknown (Guest)
on 2006-06-08 16: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.
Ola B. (Guest)
on 2006-06-08 16: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 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)
Robert K. (Guest)
on 2006-06-08 16:49
(Received via mailing list)
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
This topic is locked and can not be replied to.