I got a nil return when Dir.glob("*", File::FNM_DOTMATCH)
meet to a filepath that contains tilde like that :
~setup~.mak 253.0 B
This is the entire instruction :
l =
Dir.
glob("*", File::FNM_DOTMATCH).
tap { |a| a.shift(2) }.
map { |p,i| p.force_encoding('utf-8')}.
sort_by { |d| File.mtime(d) }.reverse!
How can I solve this?
The issue has nothing to do with tildes.
Dir::glob
does not promise to return a sorted list. I suspect your shift(2)
is intended to get rid of .
and ..
but they are not the first two items in the list. Also, you do not need to use banged version of reverse
to reverse inplace.
Reject these phantom directories explicitly:
Dir.
glob("*", File::FNM_DOTMATCH).
reject { |a| a =~ /\A\.+\z/ }. # ⇐ THIS
map { |p, i| p.force_encoding(Encoding::UTF_8) }.
sort_by { |d| File.mtime(d) }.
reverse
# $ touch ~setup~.mak
# $ ruby -e 'puts Dir.glob("*", File::FNM_DOTMATCH).reject { |a| a =~ /\A\.+\z/ }'
#⇒ ~setup~.mak