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.
One approach would be to write a little method to build a case insensitive glob for a given filename:
and then you can do:
Alternatively, you can write the directory search you suggested quite concisely. e.g.
Prior to Ruby 3.1 it was possible to use the
FNM_CASEFOLDoption to makeglobcase insensitive e.g.The documentation suggested
FNM_CASEFOLDcouldn’t be used with glob but it did actually work in older Ruby versions. However, as mentioned by lildude in the comments, the behaviour has now been brought inline with the documentation and so this approach shouldn’t be used.