Project

General

Profile

Actions

Feature #5422

closed

File.fnmatch != Dir.glob # {no,sets}

Added by sunaku (Suraj Kurapati) over 12 years ago. Updated over 11 years ago.

Status:
Closed
Assignee:
-
Target version:
[ruby-core:40037]

Description

Hello,

The File.fnmatch methods do not support Dir.glob's set notation:

Dir.glob '{.g,t}'
=> [".gem", "test"]
File.fnmatch? '{.g,t}
', 'test'
=> false
File.fnmatch? '{.g,t}*', '.gem'
=> false

Please add set notation to fnmatch() and make it equal to glob().

Thanks for your consideration.

Updated by sunaku (Suraj Kurapati) over 12 years ago

I suppose this issue could be filed as a feature request since nobody else has complained about it yet. Sorry for the wrong categorization. Thanks.

Updated by trans (Thomas Sawyer) over 12 years ago

I have. Obviously it's not earth shattering, but it really SHOULD be the same, so that glob functionality can be exactly emulated within code on arrays of strings. I've had need of this a couple of times.

Updated by drbrain (Eric Hodel) over 12 years ago

  • Tracker changed from Bug to Feature

I have moved it to the Feature tracker for you.

Updated by nobu (Nobuyoshi Nakada) over 12 years ago

=begin
What about a new method to expand braces?

File.expand_brace("{.g,t}").any? {|pat| File.fnmatch?(pat, name)}
or a shorthand
File.expand_brace("{.g,t}
") {|pat| File.fnmatch?(pat, name)}
=end

Updated by nobu (Nobuyoshi Nakada) over 12 years ago

  • Target version changed from 2.0.0 to 3.0

Updated by trans (Thomas Sawyer) over 12 years ago

Why more complexity? Is there reason that fnmatch? can't do it?

Updated by trans (Thomas Sawyer) over 12 years ago

I see. In that case, adding a new File.glob_match? (or maybe just File.match?) would be better.

File.expand_brace won't really help here b/c it would be tied to the actual file system. Part of the usefulness of fnmatch? is that it is not.

Updated by nobu (Nobuyoshi Nakada) over 12 years ago

=begin
Thomas Sawyer wrote:

I see. In that case, adding a new File.glob_match? (or maybe just File.match?) would be better.

The former doesn't sound appropriate, because "glob" does not imply brace
expansion originally.

$ case foo in f{oo,ar}) echo ok;; *) echo ng;; esac
ng

$ case "f{oo,ar}" in f{oo,ar}) echo ok;; *) echo ng;; esac
ok

File.expand_brace won't really help here b/c it would be tied to the actual file system. Part of the usefulness of fnmatch? is that it is not.

Brace expansion is a string to array conversion operation, and not used only for file names in shell scripts.
=end

Updated by trans (Thomas Sawyer) over 12 years ago

"The former doesn't sound appropriate, because "glob" does not imply brace expansion originally."

I'm only going by the functionality of Dir.<b>glob</b>. But, whatever name seems best.

"Brace expansion is a string to array conversion operation, and not used only for file names in shell scripts."

Okay, I mistook your example's use of * and similarity of name to File.expand_path to think that it was. File.expand_brace is fine if you think it would be useful in and of itself. But the real need is for a Dir.glob compatible match method.

Updated by sunaku (Suraj Kurapati) about 12 years ago

What if we pass a File::FNM_EXTGLOB flag to the File.fnmatch methods?

File.fnmatch? '{.g,t}*', '.gem', File::FNM_EXTGLOB

The name "extglob" comes from the Bash/Zsh option for extended globbing.

Updated by trans (Thomas Sawyer) about 12 years ago

Works for me if it works for the API.

Updated by sunaku (Suraj Kurapati) over 11 years ago

Thanks for your agreement Thomas.

Hey Ruby-core developers, we have agreed on the following API, where passing a File::FNM_EXTGLOB flag (the name "extglob" comes from the Bash/Zsh option for extended globbing) to the File.fnmatch methods enables support for Dir.glob's set notation.

File.fnmatch? '{.g,t}*', '.gem', File::FNM_EXTGLOB

Do you also agree on this API? Could you please implement this for us? If not, I'll try submitting a patch.

Thanks for your consideration.

Actions #14

Updated by nobu (Nobuyoshi Nakada) over 11 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r37463.
Suraj, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


dir.c: FNM_EXTGLOB

  • dir.c (file_s_fnmatch): match with expanding braces if FNM_EXTGLOB
    is set. [ruby-core:40037] [Feature #5422]
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0