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?


Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Derick Bailey

Derick Bailey is an entrepreneur, problem solver (and creator? :P ), software developer, screecaster, writer, blogger, speaker and technology leader in central Texas (north of Austin). He runs SignalLeaf.com - the amazingly awesome podcast audio hosting service that everyone should be using, and WatchMeCode.net where he throws down the JavaScript gauntlets to get you up to speed. He has been a professional software developer since the late 90's, and has been writing code since the late 80's. Find me on twitter: @derickbailey, @mutedsolutions, @backbonejsclass Find me on the web: SignalLeaf, WatchMeCode, Kendo UI blog, MarionetteJS, My Github profile, On Google+.
This entry was posted in Ruby. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://www.dust-extractors.com Dust Extractors

    There are many reasons to wanting and purchasing dust extractors. Use http://www.Dust-Extractors.com to obtain Reviews and Information about dust extractors

  • http://darrell.mozingo.net Darrell Mozingo

    I’ve done very little Ruby, but couldn’t you at least extract File::FNM_CASEFOLD to a variable named case_insensitive_search or something? I usually prefer variable/method extractions with good names rather than comments.

    • http://mutedsolutions.com Derick Bailey

      yeah, that’s what i ended up doing- used a method name as the code comments by creating a method called case_insensitive_glob

      • Mister

        Hi. I would do this:

        irb(main):052:0> Dir.entries(“.”)
        => [".", "..", "test3.tXt", "test2.TXT", "test4.text", "test1.txt"]
        irb(main):053:0> txt = Dir.glob(“*”).grep(/txt$/i)
        => ["test3.tXt", "test2.TXT", "test1.txt"]

  • http://www.zoodotcom.com/ M@

    I know this is quite late, but you could do this:

    Dir["#{ folder }**/*.{hl,Hl,HL,hL}7""].each |file|
    LabResort.import file
    end

    Sort of a middle ground, legibility-wise.