def get_type
x = [{:type=>'A', :patterns=>['foo.*']}, {:type=>'B', :patterns=>['bar.*']}]
name = 'foo.txt'
result = x.each { |item|
item[:patterns].each { |regex|
puts "Checking #{regex} against #{name}"
if !name.match(regex).nil?
puts "Found match: #{item[:type]}"
return item[:type]
end
}
}
end
result = get_type
puts "result: #{result}"
Checking foo.* against foo.txt
Found match: A
result: A
然而,所有我看到的是:
Checking foo.* against foo.txt
Found match: A
我目前的解决办法是这样的:
def get_type
x = [{:type=>'A', :patterns=>['foo.*']}, {:type=>'B', :patterns=>['bar.*']}]
name = 'foo.txt'
result = []
x.each { |item|
item[:patterns].each { |regex|
puts "Checking #{regex} against #{name}"
if !name.match(regex).nil?
puts "Found match: #{item[:type]}"
result << item[:type]
end
}
}
result[0] unless result.empty?
end
为什么第一个ap打工?或者可能是'工作',我只是不明白为什么我没有得到我所期望的。
你说得对。我还有其他一些错字。我发现的是,如果没有匹配,那么“结果”将被设置为等于'x'数组。这是我需要注意的一个条件。 – codecraig 2011-03-07 16:14:39