Last active
December 10, 2015 20:08
-
-
Save mikker/4486246 to your computer and use it in GitHub Desktop.
Hack File.fnmatch to support braces
eg. "*.{ht,x}ml"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Hack File.fnmatch to support braces | |
# eg. "*.{ht,x}ml" | |
class ::File | |
class << self | |
alias :_fnmatch :fnmatch | |
def fnmatch(pattern, path, flags = 0) | |
regex = /\{(.+)\}/i | |
# Look for braces | |
if match = pattern.match(regex) | |
# Split by commas and run fnmatch on each of | |
# the possibilities looking for matches | |
patterns = match[1].split(",").collect do |m| | |
_fnmatch(pattern.gsub(regex, m), path, flags) | |
end | |
# Return early | |
return patterns.include?(true) | |
end | |
# Fallback to original | |
_fnmatch(pattern, path, flags) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment