File.fnmatch? doesn't distinguish between .filename and ./filename ?

Discussion in 'Ruby' started by Stefano Crocco, Nov 14, 2008.

  1. I know that File.fnmatch? returns false for file names starting with a dot,
    unless you specify the File::FNM_DOTMATCH argument. The reason is that files
    starting with a dot are hidden files and so you (usually) don't want to take
    them into account.

    However, I just discovered that it also returns false for file names starting
    in ./ , for example ./xyz.rb. I think this behaviour is wrong: this isn't a
    hidden file, but a common file in the current directory. This can lead to
    unexpected (in my opinion) results. For example, look at this code:

    require 'find'
    Find.find('.'){|f| puts f if File.fnmatch('*.rb', f)}

    Since Find.find passes to the block filenames starting with ./ , no file,
    regardless of their extension, will be matched by fnmatch. This is not what
    most people expects. To make the above work, I'd either have to pass the flag
    File::FNM_DOTMATCH to the fnmatch (which would have the unwanted side effect
    of also matching hidden files) or to manually remove the leading dot from the
    file name.

    What do others think?

    Stefano
    Stefano Crocco, Nov 14, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kepioo
    Replies:
    2
    Views:
    376
    kepioo
    Apr 9, 2006
  2. abcd
    Replies:
    4
    Views:
    449
    Gabriel Genellina
    Jan 8, 2007
  3. amit
    Replies:
    4
    Views:
    339
    Keith Thompson
    Aug 11, 2006
  4. Thomas Sondergaard

    File.fnmatch and **

    Thomas Sondergaard, Apr 1, 2005, in forum: Ruby
    Replies:
    5
    Views:
    259
    Thomas Sondergaard
    Apr 4, 2005
  5. Thomas Leitner

    Dir.glob and File.fnmatch

    Thomas Leitner, May 8, 2005, in forum: Ruby
    Replies:
    3
    Views:
    158
    Thomas Leitner
    May 11, 2005
Loading...

Share This Page