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

S

Stefano Crocco

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top