Skip to content

Instantly share code, notes, and snippets.

@mikker
Last active December 10, 2015 20:08
Show Gist options
  • Save mikker/4486246 to your computer and use it in GitHub Desktop.
Save mikker/4486246 to your computer and use it in GitHub Desktop.
Hack File.fnmatch to support braces eg. "*.{ht,x}ml"
# 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