Is there a way to open a file case-insensitively in Ruby under Linux? For example, given the string foo.txt
, can I open the file FOO.txt
?
One possible way would be reading all the filenames in the directory and manually search the list for the required file, but I'm looking for a more direct method.
Whilst you can't make
open
case insensitive you can write the directory search you suggested quite concisely. e.g.Note that whilst the documentation suggests that FNM_CASEFOLD can't be used with glob this appears to be incorrect or out of date.
Alternatives
If you're concerned about using
FNM_CASEFOLD
then a couple of alternatives are:or write a little method to build a case insensitive glob for a given filename:
and then you can do:
You can use Dir.glob with the
FNM_CASEFOLD
flag to get a list of all filenames that match the given name except for case. You can then just usefirst
on the resulting array to get any result back or usemin_by
to get the one that matches the case of the orignial most closely.