Case Insensitive Dir.glob In Ruby: Really? It Has To Be That Cryptic?


I’m writing a small file importer in Ruby 1.9, to grab files form an FTP drop. The files are HL7 format, and will have a .HL7 extension. However, I had no guarantee that it would be capitalized .HL7 for the name.

The first version of the code I wrote, to get around this, was ugly:

def import(folder)
  files = []
  files << Dir.glob(folder + "**/*.HL7")
  files << Dir.glob(folder + "**/*.hl7")
  files << Dir.glob(folder + "**/*.Hl7")
  files << Dir.glob(folder + "**/*.hL7")

  files.flatten.each { |file|
    LabResult.import(file)
  }
end

After some googling, I found this post where someone is asking for the same thing, wondering if there is a way to do case insensitive Dir.globs. About halfway down the list of replies, I found what I was looking for.

Now my code is much less ugly, and much less intuitive:

def import(folder)
  files = Dir.glob(folder + "**/*.hl7", File::FNM_CASEFOLD)
  files.each { |file|
    LabResult.import(file)
  }
end

Really?! File::FNM_CASEFOLD?! Yeah… so much for Ruby’s “intuitive” and “natural language” syntax. At least it works. Now I just have to add a comment to my code to let people know what this is doing.

Any know of a better, more intuitive way of doing case insensitive Dir.globs?

A Better Solution For Partial View Controllers