# each + 検出フラグ def exclude_by_domain(exclude_url_heads, urls) urls.reject{ |url| match = false exclude_url_heads.each{ |head_re| match = true if head_re =~ url } match } end # map + any def exclude_by_domain_map_and_any(exclude_url_heads, urls) urls.reject{ |url| exclude_url_heads.map{ |head_re| head_re =~ url }.any?{ |match| match } } end # このパターンに先頭一致するものを除外したい exclude_url_heads = [ "http://example.jp/", "http://example.org/" ].map{ |head| /^#{head}/ } urls = [ "http://example.com/foo", "http://example.com/foo/bar", "http://example.jp/foo", "http://example.org/foo" ] p exclude_by_domain(exclude_url_heads, urls) #=> ["http://example.com/foo", "http://example.com/foo/bar"] p exclude_by_domain_map_and_any(exclude_url_heads, urls) #=> ["http://example.com/foo", "http://example.com/foo/bar"]
疲れてるときは each で書いてしまいそう。