how to regex a list of url paths?
I have a list of url paths:
WHITELIST_PA开发者_运维技巧THS = [ '/assets', '/images', '/javascripts']
How can regex be used to do something like:
allow_access = WHITELIST_PATHS.include? '/assets/application.css'
Idea being that the tested path just needs to start with a whitelist path. Ideas? Thanks
allow_access = WHITELIST_PATHS.any? {|p| '/assets/application.css'.start_with? p }
WHITELIST_PATHS = [ '/assets', '/images', '/javascripts']
# probably should be
# WHITELIST_PATHS = [ '/assets/', '/images/', '/javascripts/']
WHITELIST_REGEXP = /^(#{WHITELIST_PATHS.join("|")})/
allow_access = !!('/assets/application.css' =~ WHITELIST_REGEXP)
精彩评论